in order to have central place when starting with garbage collection.
import Foreign.Ptr
import Foreign.C.Types
-import Foreign.Marshal.Alloc
import Foreign.Storable
import JVM.ClassFile
import Mate.Types
import Mate.Utilities
import Mate.Debug
+import Mate.GarbageAlloc
getClassInfo :: B.ByteString -> IO ClassInfo
getClassInfo path = do
-- TODO(bernhard): we have some duplicates in immap (i.e. some
-- entries have the same offset), so we could
-- save some memory here.
- iftable <- mallocBytes ((4*) $ M.size immap)
+ iftable <- mallocClassData ((4*) $ M.size immap)
let w32_iftable = fromIntegral $ ptrToIntPtr iftable :: Word32
-- store interface-table at offset 0 in method-table
pokeElemOff (intPtrToPtr $ fromIntegral mbase) 0 w32_iftable
let (sfields, ifields) = span (S.member ACC_STATIC . fieldAccessFlags) (classFields cf)
- staticbase <- mallocBytes ((fromIntegral $ length sfields) * 4)
+ staticbase <- mallocClassData ((fromIntegral $ length sfields) * 4)
let i_sb = fromIntegral $ ptrToIntPtr $ staticbase
let sm = zipbase i_sb sfields
let sc_sm = getsupermap superclass ciStaticMap
let methodmap = (M.fromList mm) `M.union` sc_mm
-- (+1): one slot for the interface-table-ptr
- methodbase <- mallocBytes (((+1) $ fromIntegral $ M.size methodmap) * 4)
+ methodbase <- mallocClassData (((+1) $ fromIntegral $ M.size methodmap) * 4)
return (methodmap, fromIntegral $ ptrToIntPtr $ methodbase)
where zipbase base = zipWith (\x y -> (entry y, x + base)) [0,4..]
where entry y = (methodName y) `B.append` (encode $ methodSignature y)
--- /dev/null
+module Mate.GarbageAlloc where
+
+import Foreign
+import Foreign.C
+
+-- unified place for allocating Memory
+-- TODO: implement GC stuff ;-)
+
+mallocClassData :: Int -> IO (Ptr a)
+mallocClassData = mallocBytes
+
+mallocString :: Int -> IO (Ptr a)
+mallocString = mallocBytes
+
+foreign export ccall mallocObject :: Int -> IO CUInt
+mallocObject :: Int -> IO CUInt
+mallocObject size = do
+ ptr <- mallocBytes size
+ return $ fromIntegral $ ptrToIntPtr ptr
#endif
import Foreign.Ptr
-import Foreign.Marshal.Alloc
import Foreign.Marshal.Utils
import Foreign.Marshal.Array
import Mate.Types
import Mate.Debug
+import Mate.GarbageAlloc
getUniqueStringAddr :: B.ByteString -> IO Word32
let strlen = fromIntegral $ B.length str
arr <- newArray $ ((map fromIntegral $ B.unpack str) :: [Word8])
-- (+1) for \0
- newstr <- mallocBytes (strlen + 1)
+ newstr <- mallocString (strlen + 1)
BI.memset newstr 0 (fromIntegral $ strlen + 1)
copyBytes newstr arr strlen
let w32_ptr = fromIntegral $ ptrToIntPtr newstr
foreign import ccall "dynamic"
code_int :: FunPtr (CInt -> CInt -> IO CInt) -> (CInt -> CInt -> IO CInt)
-foreign import ccall "getMallocAddr"
- getMallocAddr :: CUInt
+foreign import ccall "getMallocObjectAddr"
+ getMallocObjectAddr :: CUInt
foreign import ccall "register_signal"
register_signal :: IO ()
callMalloc = do
calladdr <- getCurrentOffset
let w32_calladdr = 5 + calladdr
- let malloaddr = (fromIntegral getMallocAddr :: Word32)
+ let malloaddr = (fromIntegral getMallocObjectAddr :: Word32)
call (malloaddr - w32_calladdr)
add esp (4 :: Word32)
push eax
unsigned int getMethodEntry(unsigned int, unsigned int);
unsigned int getStaticFieldAddr(unsigned int, void*);
unsigned int getTrapType(unsigned int, unsigned int);
+unsigned int mallocObject(int);
#ifdef DBG_TRAP
#define dprintf(args...) do { printf (args); } while (0);
return (unsigned int) mainresult;
}
-unsigned int getMallocAddr(void)
+unsigned int getMallocObjectAddr(void)
{
- return (unsigned int) malloc;
+ return (unsigned int) mallocObject;
}