- if (!thread_needLock())
- {
- /* Then we haven't locked it yet. Be sure
- * and lock for a collection, since a finalizer
- * may start a new thread.
- */
- synchronized (lock)
- {
- this.fullcollectshell();
- }
- }
- else if (!this.fullcollectshell()) // collect to find a new page
- {
- //this.newPool(1);
- }
- }
- if (!this.bucket[bin] && !this.allocPage(bin))
- {
- this.newPool(1); // allocate new pool to find a new page
- int result = this.allocPage(bin);
- if (!result)
- onOutOfMemoryError();
- }
- p = this.bucket[bin];
- }
- capacity = binsize[bin];
-
- // Return next item from free list
- this.bucket[bin] = (cast(List*)p).next;
- if (!(attrs & BlkAttr.NO_SCAN))
- memset(p + size, 0, capacity - size);
- if (opts.options.mem_stomp)
- memset(p, 0xF0, size);
- }
- else
- {
- p = this.bigAlloc(size);
- if (!p)
- onOutOfMemoryError();
- // Round the size up to the number of pages needed to store it
- size_t npages = (size + PAGESIZE - 1) / PAGESIZE;
- capacity = npages * PAGESIZE;
- }
-
- // Store the bit mask AFTER SENTINEL_POST
- // TODO: store it BEFORE, so the bitmask is protected too
- if (has_pm) {
- auto end_of_blk = cast(size_t**)(p + capacity - size_t.sizeof);
- *end_of_blk = pm_bitmask;
- size -= size_t.sizeof;
- }
-
- if (opts.options.sentinel) {
- size -= SENTINEL_EXTRA;
- p = sentinel_add(p);
- sentinel_init(p, size);
- }
-
- if (attrs)
- {
- Pool *pool = this.findPool(p);
- assert(pool);
-
- this.setAttr(pool, cast(size_t)(p - pool.baseAddr) / 16, attrs);
- }
- return p;
- }
-
-
- /**
- *
- */
- void *calloc(size_t size, uint attrs, PointerMap ptrmap)
- {
- if (!size)
- {
- return null;
- }
-
- if (!thread_needLock())
- {
- return callocNoSync(size, attrs, ptrmap.bits.ptr);
- }
- else synchronized (lock)
- {
- return callocNoSync(size, attrs, ptrmap.bits.ptr);
- }
- }
-
-
- //
- //
- //
- private void *callocNoSync(size_t size, uint attrs, size_t* pm_bitmask)
- {
- assert(size != 0);
-
- void *p = mallocNoSync(size, attrs, pm_bitmask);
- memset(p, 0, size);
- return p;
- }
-
-
- /**
- *
- */
- void *realloc(void *p, size_t size, uint attrs, PointerMap ptrmap)
- {
- if (!thread_needLock())
- {
- return reallocNoSync(p, size, attrs, ptrmap.bits.ptr);
- }
- else synchronized (lock)
- {
- return reallocNoSync(p, size, attrs, ptrmap.bits.ptr);
- }
- }
-
-
- //
- //
- //
- private void *reallocNoSync(void *p, size_t size, uint attrs,
- size_t* pm_bitmask)
- {
- if (!size)
- {
- if (p)
- {
- freeNoSync(p);
- p = null;
- }
- }
- else if (!p)
- {
- p = mallocNoSync(size, attrs, pm_bitmask);
- }
- else
- {
- Pool* pool = this.findPool(p);
- if (pool is null)
- return null;
-
- // Set or retrieve attributes as appropriate
- auto bit_i = cast(size_t)(p - pool.baseAddr) / 16;
- if (attrs) {
- this.clrAttr(pool, bit_i, BlkAttr.ALL_BITS);
- this.setAttr(pool, bit_i, attrs);
- }
- else
- attrs = this.getAttr(pool, bit_i);
-
- void* blk_base_addr = this.findBase(p);
- size_t blk_size = this.findSize(p);
- bool has_pm = has_pointermap(attrs);
- size_t pm_bitmask_size = 0;
- if (has_pm) {
- pm_bitmask_size = size_t.sizeof;
- // Retrieve pointer map bit mask if appropriate
- if (pm_bitmask is null) {
- auto end_of_blk = cast(size_t**)(blk_base_addr +
- blk_size - size_t.sizeof);
- pm_bitmask = *end_of_blk;
- }
- }
-
- if (opts.options.sentinel)
- {
- sentinel_Invariant(p);
- size_t sentinel_stored_size = *sentinel_size(p);
- if (sentinel_stored_size != size)
- {
- void* p2 = mallocNoSync(size, attrs, pm_bitmask);
- if (sentinel_stored_size < size)
- size = sentinel_stored_size;
- cstring.memcpy(p2, p, size);
- p = p2;
- }
- }
- else
- {
- size += pm_bitmask_size;
- if (blk_size >= PAGESIZE && size >= PAGESIZE)
- {
- auto psz = blk_size / PAGESIZE;
- auto newsz = (size + PAGESIZE - 1) / PAGESIZE;
- if (newsz == psz)
- return p;
-
- auto pagenum = (p - pool.baseAddr) / PAGESIZE;
-
- if (newsz < psz)
- {
- // Shrink in place
- synchronized (lock)
- {
- if (opts.options.mem_stomp)
- memset(p + size - pm_bitmask_size, 0xF2,
- blk_size - size - pm_bitmask_size);
- pool.freePages(pagenum + newsz, psz - newsz);
- }
- if (has_pm) {
- auto end_of_blk = cast(size_t**)(
- blk_base_addr + (PAGESIZE * newsz) -
- pm_bitmask_size);
- *end_of_blk = pm_bitmask;
- }
- return p;
- }
- else if (pagenum + newsz <= pool.npages)