Changeset: 150f3d17a2ef for MonetDB URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=150f3d17a2ef Modified Files: gdk/gdk_heap.c Branch: Feb2013 Log Message:
No need to dynamically allocate the heap cache structure. diffs (133 lines): diff --git a/gdk/gdk_heap.c b/gdk/gdk_heap.c --- a/gdk/gdk_heap.c +++ b/gdk/gdk_heap.c @@ -78,32 +78,30 @@ typedef struct heap_cache { heap_cache_e *hc; } heap_cache; -static heap_cache *hc = NULL; +static heap_cache hc; static MT_Lock HEAPcacheLock; void HEAPcacheInit(void) { #if HEAP_CACHE_SIZE > 0 - if (!hc) { - int i; + int i; - MT_lock_init(&HEAPcacheLock, "HEAPcache_init"); - MT_lock_set(&HEAPcacheLock, "HEAPcache_init"); - hc = (heap_cache *) GDKmalloc(sizeof(heap_cache)); - hc->used = 0; - hc->sz = HEAP_CACHE_SIZE; - hc->hc = (heap_cache_e *) GDKmalloc(sizeof(heap_cache_e) * hc->sz); - GDKcreatedir(HCDIR DIR_SEP_STR); - /* clean old leftovers */ - for (i = 0; i < HEAP_CACHE_SIZE; i++) { - char fn[PATHLENGTH]; + assert(hc.sz == 0); + MT_lock_init(&HEAPcacheLock, "HEAPcache_init"); + MT_lock_set(&HEAPcacheLock, "HEAPcache_init"); + hc.used = 0; + hc.sz = HEAP_CACHE_SIZE; + hc.hc = (heap_cache_e *) GDKmalloc(sizeof(heap_cache_e) * hc.sz); + GDKcreatedir(HCDIR DIR_SEP_STR); + /* clean old leftovers */ + for (i = 0; i < HEAP_CACHE_SIZE; i++) { + char fn[PATHLENGTH]; - snprintf(fn, PATHLENGTH, "%d", i); - GDKunlink(HCDIR, fn, NULL); - } - MT_lock_unset(&HEAPcacheLock, "HEAPcache_init"); + snprintf(fn, PATHLENGTH, "%d", i); + GDKunlink(HCDIR, fn, NULL); } + MT_lock_unset(&HEAPcacheLock, "HEAPcache_init"); #endif } @@ -114,12 +112,12 @@ HEAPcacheAdd(void *base, size_t maxsz, c MT_lock_set(&HEAPcacheLock, "HEAPcache_init"); - if (hc && free_file && fn && storage == STORE_MMAP && hc->used < hc->sz) { - heap_cache_e *e = hc->hc + hc->used; + if (free_file && fn && storage == STORE_MMAP && hc.used < hc.sz) { + heap_cache_e *e = hc.hc + hc.used; e->base = base; e->maxsz = maxsz; - snprintf(e->fn, PATHLENGTH, "%d", hc->used); + snprintf(e->fn, PATHLENGTH, "%d", hc.used); GDKunlink(HCDIR, e->fn, NULL); added = 1; if (GDKmove(BATDIR, fn, NULL, HCDIR, e->fn, NULL) < 0) { @@ -133,12 +131,12 @@ HEAPcacheAdd(void *base, size_t maxsz, c added = 0; } if (added) - hc->used++; + hc.used++; } MT_lock_unset(&HEAPcacheLock, "HEAPcache_init"); if (!added) return GDKmunmap(base, maxsz); - HEAPDEBUG fprintf(stderr, "#HEAPcacheAdd (%s) " SZFMT " " PTRFMT " %d %d %d\n", fn, maxsz, PTRFMTCAST base, (int) storage, free_file, hc->used); + HEAPDEBUG fprintf(stderr, "#HEAPcacheAdd (%s) " SZFMT " " PTRFMT " %d %d %d\n", fn, maxsz, PTRFMTCAST base, (int) storage, free_file, hc.used); return 0; } @@ -149,23 +147,23 @@ HEAPcacheFind(size_t *maxsz, char *fn, s *maxsz = (1 + (*maxsz >> 16)) << 16; /* round up to 64K */ MT_lock_set(&HEAPcacheLock, "HEAPcache_init"); - if (hc && mode == STORE_MMAP && hc->used > 0) { + if (mode == STORE_MMAP && hc.used > 0) { int i; heap_cache_e *e = NULL; size_t cursz = 0; - HEAPDEBUG fprintf(stderr, "#HEAPcacheFind (%s)" SZFMT " %d %d\n", fn, *maxsz, (int) mode, hc->used); + HEAPDEBUG fprintf(stderr, "#HEAPcacheFind (%s)" SZFMT " %d %d\n", fn, *maxsz, (int) mode, hc.used); /* find best match: prefer smallest larger than or * equal to requested, otherwise largest smaller than * requested */ - for (i = 0; i < hc->used; i++) { - if ((hc->hc[i].maxsz >= *maxsz && - (e == NULL || hc->hc[i].maxsz < cursz || cursz < *maxsz)) || - (hc->hc[i].maxsz < *maxsz && + for (i = 0; i < hc.used; i++) { + if ((hc.hc[i].maxsz >= *maxsz && + (e == NULL || hc.hc[i].maxsz < cursz || cursz < *maxsz)) || + (hc.hc[i].maxsz < *maxsz && cursz < *maxsz && - hc->hc[i].maxsz > cursz)) { - e = hc->hc + i; + hc.hc[i].maxsz > cursz)) { + e = hc.hc + i; cursz = e->maxsz; } } @@ -199,12 +197,12 @@ HEAPcacheFind(size_t *maxsz, char *fn, s } } if (e != NULL) { - hc->used--; - i = (int) (e - hc->hc); - if (i < hc->used) { - e->base = hc->hc[hc->used].base; - e->maxsz = hc->hc[hc->used].maxsz; - GDKmove(HCDIR, hc->hc[hc->used].fn, NULL, HCDIR, e->fn, NULL); + hc.used--; + i = (int) (e - hc.hc); + if (i < hc.used) { + e->base = hc.hc[hc.used].base; + e->maxsz = hc.hc[hc.used].maxsz; + GDKmove(HCDIR, hc.hc[hc.used].fn, NULL, HCDIR, e->fn, NULL); } } } _______________________________________________ checkin-list mailing list checkin-list@monetdb.org http://mail.monetdb.org/mailman/listinfo/checkin-list