From: Leandro Lucarella Date: Sat, 16 Jan 2010 00:29:31 +0000 (-0300) Subject: Remove Tango dependency X-Git-Url: https://git.llucax.com/software/dgc/cdgc.git/commitdiff_plain/f102613fc4eccc2bc732885bf78aeaa8804b9208 Remove Tango dependency To avoid Tango dependency, we need to write our own C-API interface. This is done in the new gc.libc module. In the future, maybe this module will use Tango or Phobos accordly, but for now we stay free of dependencies (at the expense of some extra work). --- diff --git a/Makefile b/Makefile index b41b9a0..32232ef 100644 --- a/Makefile +++ b/Makefile @@ -36,6 +36,7 @@ sources := \ gc/alloc.d \ gc/bits.d \ gc/stats.d \ + gc/libc.d \ gc/gc.d # Default target diff --git a/gc/alloc.d b/gc/alloc.d index cad30f2..55d0497 100644 --- a/gc/alloc.d +++ b/gc/alloc.d @@ -26,31 +26,57 @@ module gc.alloc; -version (Win32) -{ - import tango.sys.win32.UserGdi; - - alias int pthread_t; - - pthread_t pthread_self() - { - return cast(pthread_t) GetCurrentThreadId(); +// C OS-specific API +private extern (C) { + version (Win32) { + alias void* POINTER; + alias POINTER LPVOID; + alias uint DWORD; + alias int WINBOOL; + enum: DWORD { + PAGE_READWRITE = 4, + MEM_RESERVE = 8192, + MEM_COMMIT = 4096, + MEM_DECOMMIT = 16384, + MEM_RELEASE = 32768, + } + LPVOID VirtualAlloc(LPVOID, DWORD, DWORD, DWORD); + WINBOOL VirtualFree(LPVOID, DWORD, DWORD); + } + else version (Posix) { + version (linux) enum: bool { OPTIONAL_LARGEFILE_SUPPORT = true } + else version (solaris) enum: bool { OPTIONAL_LARGEFILE_SUPPORT = true } + else enum: bool { OPTIONAL_LARGEFILE_SUPPORT = false } + static if (OPTIONAL_LARGEFILE_SUPPORT) + enum: bool { USE_LARGEFILE64 = ((void*).sizeof == 4) } + else + enum: bool { USE_LARGEFILE64 = false } + static if (USE_LARGEFILE64 || (void*).sizeof > int.sizeof) + alias long off_t; + else + alias int off_t; + enum: int { + PROT_NONE = 0x0, + PROT_READ = 0x1, + PROT_WRITE = 0x2, + PROT_EXEC = 0x4, + MAP_SHARED = 0x01, + MAP_PRIVATE = 0x02, + MAP_FIXED = 0x10, + } + const MAP_FAILED = cast(void*) -1; + // Non-standard, but needed + version (linux) { enum: int { MAP_ANON = 0x20 } } + else version (darwin) { enum: int { MAP_ANON = 0x1000 } } + else version (freebsd) { enum: int { MAP_ANON = 0x1000 } } + else version (solaris) { enum: int { MAP_ANON = 0x100 } } + void* mmap(void*, size_t, int, int, int, off_t); + int munmap(void*, size_t); + } + else { + // Standard C library + import gc.libc; } - - //version = GC_Use_Alloc_Win32; -} -else version (Posix) -{ - import tango.stdc.posix.sys.mman; - import tango.stdc.stdlib; - - //version = GC_Use_Alloc_MMap; -} -else -{ - import tango.stdc.stdlib; - - //version = GC_Use_Alloc_Malloc; } /+ diff --git a/gc/bits.d b/gc/bits.d index e8e9263..e9ece9b 100644 --- a/gc/bits.d +++ b/gc/bits.d @@ -26,9 +26,7 @@ module gc.bits; -import tango.core.BitManip; -import tango.stdc.string; -import tango.stdc.stdlib; +import gc.libc; private extern (C) void onOutOfMemoryError(); diff --git a/gc/gc.d b/gc/gc.d index 7915a11..8cd5609 100644 --- a/gc/gc.d +++ b/gc/gc.d @@ -50,11 +50,8 @@ version = MULTI_THREADED; // produce multithreaded version import gc.bits; import gc.stats; import gc.alloc; +import gc.libc; -import cstdlib = tango.stdc.stdlib : calloc, free, malloc, realloc; -import cstring = tango.stdc.string : memcpy, memmove, memset; - -debug import tango.stdc.stdio : printf; version (GNU) { @@ -147,7 +144,7 @@ debug (LOGGING) void Dtor() { if (data) - cstdlib.free(data); + .free(data); data = null; } @@ -160,18 +157,18 @@ debug (LOGGING) assert(dim + nentries <= allocdim); if (!data) { - data = cast(Log*)cstdlib.malloc(allocdim * Log.sizeof); + data = cast(Log*) .malloc(allocdim * Log.sizeof); if (!data && allocdim) onOutOfMemoryError(); } else { Log *newdata; - newdata = cast(Log*)cstdlib.malloc(allocdim * Log.sizeof); + newdata = cast(Log*) .malloc(allocdim * Log.sizeof); if (!newdata && allocdim) onOutOfMemoryError(); - cstring.memcpy(newdata, data, dim * Log.sizeof); - cstdlib.free(data); + .memcpy(newdata, data, dim * Log.sizeof); + .free(data); data = newdata; } } @@ -186,7 +183,7 @@ debug (LOGGING) void remove(size_t i) { - cstring.memmove(data + i, data + i + 1, (dim - i) * Log.sizeof); + .memmove(data + i, data + i + 1, (dim - i) * Log.sizeof); dim--; } @@ -206,7 +203,7 @@ debug (LOGGING) { reserve(from.dim - dim); assert(from.dim <= allocdim); - cstring.memcpy(data, from.data, from.dim * Log.sizeof); + .memcpy(data, from.data, from.dim * Log.sizeof); dim = from.dim; } } @@ -237,7 +234,7 @@ class GC void initialize() { gcLock = GCLock.classinfo; - gcx = cast(Gcx*)cstdlib.calloc(1, Gcx.sizeof); + gcx = cast(Gcx*) .calloc(1, Gcx.sizeof); if (!gcx) onOutOfMemoryError(); gcx.initialize(); @@ -250,7 +247,7 @@ class GC if (gcx) { gcx.Dtor(); - cstdlib.free(gcx); + .free(gcx); gcx = null; } } @@ -483,9 +480,9 @@ class GC // Return next item from free list gcx.bucket[bin] = (cast(List*)p).next; if( !(bits & BlkAttr.NO_SCAN) ) - cstring.memset(p + size, 0, binsize[bin] - size); + .memset(p + size, 0, binsize[bin] - size); //debug(PRINTF) printf("\tmalloc => %x\n", p); - debug (MEMSTOMP) cstring.memset(p, 0xF0, size); + debug (MEMSTOMP) .memset(p, 0xF0, size); } else { @@ -539,7 +536,7 @@ class GC //debug(PRINTF) printf("calloc: %x len %d\n", p, len); void *p = mallocNoSync(size, bits); - cstring.memset(p, 0, size); + .memset(p, 0, size); return p; } @@ -609,7 +606,7 @@ class GC if (psize < size) size = psize; //debug(PRINTF) printf("\tcopying %d bytes\n",size); - cstring.memcpy(p2, p, size); + .memcpy(p2, p, size); p = p2; } } @@ -630,7 +627,7 @@ class GC { // Shrink in place synchronized (gcLock) { - debug (MEMSTOMP) cstring.memset(p + size, 0xF2, psize - size); + debug (MEMSTOMP) .memset(p + size, 0xF2, psize - size); pool.freePages(pagenum + newsz, psz - newsz); } return p; @@ -644,8 +641,8 @@ class GC { if (i == pagenum + newsz) { - debug (MEMSTOMP) cstring.memset(p + psize, 0xF0, size - psize); - cstring.memset(&pool.pagetable[pagenum + psz], B_PAGEPLUS, newsz - psz); + debug (MEMSTOMP) .memset(p + psize, 0xF0, size - psize); + .memset(&pool.pagetable[pagenum + psz], B_PAGEPLUS, newsz - psz); return p; } if (i == pool.ncommitted) @@ -689,7 +686,7 @@ class GC if (psize < size) size = psize; //debug(PRINTF) printf("\tcopying %d bytes\n",size); - cstring.memcpy(p2, p, size); + .memcpy(p2, p, size); p = p2; } } @@ -770,8 +767,8 @@ class GC } else return 0; - debug (MEMSTOMP) cstring.memset(p + psize, 0xF0, (psz + sz) * PAGESIZE - psize); - cstring.memset(pool.pagetable + pagenum + psz, B_PAGEPLUS, sz); + debug (MEMSTOMP) .memset(p + psize, 0xF0, (psz + sz) * PAGESIZE - psize); + .memset(pool.pagetable + pagenum + psz, B_PAGEPLUS, sz); gcx.p_cache = null; gcx.size_cache = 0; return (psz + sz) * PAGESIZE; @@ -864,14 +861,14 @@ class GC n = pagenum; while (++n < pool.ncommitted && pool.pagetable[n] == B_PAGEPLUS) npages++; - debug (MEMSTOMP) cstring.memset(p, 0xF2, npages * PAGESIZE); + debug (MEMSTOMP) .memset(p, 0xF2, npages * PAGESIZE); pool.freePages(pagenum, npages); } else { // Add to free list List *list = cast(List*)p; - debug (MEMSTOMP) cstring.memset(p, 0xF2, binsize[bin]); + debug (MEMSTOMP) .memset(p, 0xF2, binsize[bin]); list.next = gcx.bucket[bin]; gcx.bucket[bin] = list; @@ -1288,7 +1285,7 @@ class GC size_t bsize = 0; //debug(PRINTF) printf("getStats()\n"); - cstring.memset(&stats, 0, GCStats.sizeof); + .memset(&stats, 0, GCStats.sizeof); for (n = 0; n < gcx.npools; n++) { Pool *pool = gcx.pooltable[n]; @@ -1424,16 +1421,16 @@ struct Gcx { Pool *pool = pooltable[i]; pool.Dtor(); - cstdlib.free(pool); + .free(pool); } if (pooltable) - cstdlib.free(pooltable); + .free(pooltable); if (roots) - cstdlib.free(roots); + .free(roots); if (ranges) - cstdlib.free(ranges); + .free(ranges); } @@ -1504,12 +1501,12 @@ struct Gcx size_t newdim = rootdim * 2 + 16; void** newroots; - newroots = cast(void**)cstdlib.malloc(newdim * newroots[0].sizeof); + newroots = cast(void**) .malloc(newdim * newroots[0].sizeof); if (!newroots) onOutOfMemoryError(); if (roots) - { cstring.memcpy(newroots, roots, nroots * newroots[0].sizeof); - cstdlib.free(roots); + { .memcpy(newroots, roots, nroots * newroots[0].sizeof); + .free(roots); } roots = newroots; rootdim = newdim; @@ -1529,7 +1526,7 @@ struct Gcx if (roots[i] == p) { nroots--; - cstring.memmove(roots + i, roots + i + 1, (nroots - i) * roots[0].sizeof); + .memmove(roots + i, roots + i + 1, (nroots - i) * roots[0].sizeof); return; } } @@ -1549,12 +1546,12 @@ struct Gcx size_t newdim = rangedim * 2 + 16; Range *newranges; - newranges = cast(Range*)cstdlib.malloc(newdim * newranges[0].sizeof); + newranges = cast(Range*) .malloc(newdim * newranges[0].sizeof); if (!newranges) onOutOfMemoryError(); if (ranges) - { cstring.memcpy(newranges, ranges, nranges * newranges[0].sizeof); - cstdlib.free(ranges); + { .memcpy(newranges, ranges, nranges * newranges[0].sizeof); + .free(ranges); } ranges = newranges; rangedim = newdim; @@ -1577,7 +1574,7 @@ struct Gcx if (ranges[i].pbot == pbot) { nranges--; - cstring.memmove(ranges + i, ranges + i + 1, (nranges - i) * ranges[0].sizeof); + .memmove(ranges + i, ranges + i + 1, (nranges - i) * ranges[0].sizeof); return; } } @@ -1845,8 +1842,8 @@ struct Gcx continue; } pool.Dtor(); - cstdlib.free(pool); - cstring.memmove(pooltable + n, + .free(pool); + .memmove(pooltable + n, pooltable + n + 1, (--npools - n) * (Pool*).sizeof); minAddr = pooltable[0].baseAddr; @@ -1929,10 +1926,10 @@ struct Gcx L1: pool.pagetable[pn] = B_PAGE; if (npages > 1) - cstring.memset(&pool.pagetable[pn + 1], B_PAGEPLUS, npages - 1); + .memset(&pool.pagetable[pn + 1], B_PAGEPLUS, npages - 1); p = pool.baseAddr + pn * PAGESIZE; - cstring.memset(cast(char *)p + size, 0, npages * PAGESIZE - size); - debug (MEMSTOMP) cstring.memset(p, 0xF1, size); + .memset(cast(char *)p + size, 0, npages * PAGESIZE - size); + debug (MEMSTOMP) .memset(p, 0xF1, size); //debug(PRINTF) printf("\tp = %x\n", p); return p; @@ -1980,7 +1977,7 @@ struct Gcx npages = n; } - pool = cast(Pool *)cstdlib.calloc(1, Pool.sizeof); + pool = cast(Pool *) .calloc(1, Pool.sizeof); if (pool) { pool.initialize(npages); @@ -1988,7 +1985,7 @@ struct Gcx goto Lerr; newnpools = npools + 1; - newpooltable = cast(Pool **)cstdlib.realloc(pooltable, newnpools * (Pool *).sizeof); + newpooltable = cast(Pool **) .realloc(pooltable, newnpools * (Pool *).sizeof); if (!newpooltable) goto Lerr; @@ -1998,7 +1995,7 @@ struct Gcx if (pool.opCmp(newpooltable[i]) < 0) break; } - cstring.memmove(newpooltable + i + 1, newpooltable + i, (npools - i) * (Pool *).sizeof); + .memmove(newpooltable + i + 1, newpooltable + i, (npools - i) * (Pool *).sizeof); newpooltable[i] = pool; pooltable = newpooltable; @@ -2011,7 +2008,7 @@ struct Gcx Lerr: pool.Dtor(); - cstdlib.free(pool); + .free(pool); return null; } @@ -2403,7 +2400,7 @@ struct Gcx //debug(PRINTF) printf("\tcollecting %x\n", list); log_free(sentinel_add(list)); - debug (MEMSTOMP) cstring.memset(p, 0xF3, size); + debug (MEMSTOMP) .memset(p, 0xF3, size); } pool.pagetable[pn] = B_FREE; freed += PAGESIZE; @@ -2426,7 +2423,7 @@ struct Gcx debug(PRINTF) printf("\tcollecting %x\n", list); log_free(sentinel_add(list)); - debug (MEMSTOMP) cstring.memset(p, 0xF3, size); + debug (MEMSTOMP) .memset(p, 0xF3, size); freed += size; } @@ -2447,7 +2444,7 @@ struct Gcx log_free(sentinel_add(p)); pool.pagetable[pn] = B_FREE; freedpages++; - debug (MEMSTOMP) cstring.memset(p, 0xF3, PAGESIZE); + debug (MEMSTOMP) .memset(p, 0xF3, PAGESIZE); while (pn + 1 < ncommitted && pool.pagetable[pn + 1] == B_PAGEPLUS) { pn++; @@ -2456,7 +2453,7 @@ struct Gcx debug (MEMSTOMP) { p += PAGESIZE; - cstring.memset(p, 0xF3, PAGESIZE); + .memset(p, 0xF3, PAGESIZE); } } } @@ -2775,10 +2772,10 @@ struct Pool freebits.alloc(cast(size_t)poolsize / 16); noscan.alloc(cast(size_t)poolsize / 16); - pagetable = cast(ubyte*)cstdlib.malloc(npages); + pagetable = cast(ubyte*) .malloc(npages); if (!pagetable) onOutOfMemoryError(); - cstring.memset(pagetable, B_UNCOMMITTED, npages); + .memset(pagetable, B_UNCOMMITTED, npages); this.npages = npages; ncommitted = 0; @@ -2809,7 +2806,7 @@ struct Pool topAddr = null; } if (pagetable) - cstdlib.free(pagetable); + .free(pagetable); mark.Dtor(); scan.Dtor(); @@ -2890,7 +2887,7 @@ struct Pool //fflush(stdout); if (os_mem_commit(baseAddr, ncommitted * PAGESIZE, tocommit * PAGESIZE) == 0) { - cstring.memset(pagetable + ncommitted, B_FREE, tocommit); + .memset(pagetable + ncommitted, B_FREE, tocommit); auto i = ncommitted; ncommitted += tocommit; @@ -2911,7 +2908,7 @@ struct Pool */ void freePages(size_t pagenum, size_t npages) { - cstring.memset(&pagetable[pagenum], B_FREE, npages); + .memset(&pagetable[pagenum], B_FREE, npages); } diff --git a/gc/iface.d b/gc/iface.d index efef61b..6f95c5d 100644 --- a/gc/iface.d +++ b/gc/iface.d @@ -28,7 +28,7 @@ module gc.iface; import gc.gc; import gc.stats; -import tango.stdc.stdlib; +import gc.c; version=GCCLASS; @@ -67,13 +67,13 @@ extern (C) void gc_init() { void* p; ClassInfo ci = GC.classinfo; - p = tango.stdc.stdlib.malloc(ci.init.length); + p = malloc(ci.init.length); (cast(byte*)p)[0 .. ci.init.length] = ci.init[]; _gc = cast(GC)p; } else { - _gc = cast(GC*) tango.stdc.stdlib.calloc(1, GC.sizeof); + _gc = cast(GC*) calloc(1, GC.sizeof); } _gc.initialize(); version (DigitalMars) version(OSX) { @@ -96,7 +96,7 @@ extern (C) void gc_term() // Often this probably doesn't matter much since the app is // supposed to be shutting down anyway, but for example tests might // crash (and be considerd failed even if the test was ok). - // thus this is not the default and should be enabled by + // thus this is not the default and should be enabled by // I'm disabling cleanup for now until I can think about it some // more. // diff --git a/gc/libc.d b/gc/libc.d new file mode 100644 index 0000000..e0cd23e --- /dev/null +++ b/gc/libc.d @@ -0,0 +1,29 @@ + +module gc.c; + +version (Windows) { + alias int c_long; + alias uint c_ulong; +} +else { + static if ((void*).sizeof > int.sizeof) { + alias long c_long; + alias ulong c_ulong; + } + else { + alias int c_long; + alias uint c_ulong; + } +} + +// C standard library +extern (C): +void* realloc(void*, size_t); +void* malloc(size_t); +void* calloc(size_t, size_t); +void free(void*); +void* memset(void*, int, size_t); +void* memcpy(void*, void*, size_t); +void* memmove(void*, void*, size_t); +void printf(char* fmt, ...); +