alloc_sarray (j_common_ptr cinfo, int pool_id, JDIMENSION samplesperrow, JDIMENSION numrows) /* Allocate a 2-D sample array */ { my_mem_ptr mem = (my_mem_ptr) cinfo->mem; JSAMPARRAY result; JSAMPROW workspace; JDIMENSION rowsperchunk, currow, i; long ltemp; /* Make sure each row is properly aligned */ if ((ALIGN_SIZE % sizeof(JSAMPLE)) != 0) out_of_memory(cinfo, 5); /* safety check */ if (samplesperrow > MAX_ALLOC_CHUNK) { /* This prevents overflow/wrap-around in round_up_pow2() if sizeofobject is close to SIZE_MAX. */ out_of_memory(cinfo, 9); } samplesperrow = (JDIMENSION)round_up_pow2(samplesperrow, (2 * ALIGN_SIZE) / sizeof(JSAMPLE)); /* Calculate max # of rows allowed in one allocation chunk */ ltemp = (MAX_ALLOC_CHUNK-sizeof(large_pool_hdr)) / ((long) samplesperrow * sizeof(JSAMPLE)); if (ltemp <= 0) ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); if (ltemp < (long) numrows) rowsperchunk = (JDIMENSION) ltemp; else rowsperchunk = numrows; mem->last_rowsperchunk = rowsperchunk; /* Get space for row pointers (small object) */ result = (JSAMPARRAY) alloc_small(cinfo, pool_id, (size_t) (numrows * sizeof(JSAMPROW))); /* Get the rows themselves (large objects) */ currow = 0; while (currow < numrows) { rowsperchunk = MIN(rowsperchunk, numrows - currow); workspace = (JSAMPROW) alloc_large(cinfo, pool_id, (size_t) ((size_t) rowsperchunk * (size_t) samplesperrow * sizeof(JSAMPLE))); for (i = rowsperchunk; i > 0; i--) { result[currow++] = workspace; workspace += samplesperrow; } } return result; }
alloc_barray (j_common_ptr cinfo, int pool_id, JDIMENSION blocksperrow, JDIMENSION numrows) /* Allocate a 2-D coefficient-block array */ { my_mem_ptr mem = (my_mem_ptr) cinfo->mem; JBLOCKARRAY result; JBLOCKROW workspace; JDIMENSION rowsperchunk, currow, i; long ltemp; /* Make sure each row is properly aligned */ if ((sizeof(JBLOCK) % ALIGN_SIZE) != 0) out_of_memory(cinfo, 6); /* safety check */ /* Calculate max # of rows allowed in one allocation chunk */ ltemp = (MAX_ALLOC_CHUNK-sizeof(large_pool_hdr)) / ((long) blocksperrow * sizeof(JBLOCK)); if (ltemp <= 0) ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); if (ltemp < (long) numrows) rowsperchunk = (JDIMENSION) ltemp; else rowsperchunk = numrows; mem->last_rowsperchunk = rowsperchunk; /* Get space for row pointers (small object) */ result = (JBLOCKARRAY) alloc_small(cinfo, pool_id, (size_t) (numrows * sizeof(JBLOCKROW))); /* Get the rows themselves (large objects) */ currow = 0; while (currow < numrows) { rowsperchunk = MIN(rowsperchunk, numrows - currow); workspace = (JBLOCKROW) alloc_large(cinfo, pool_id, (size_t) ((size_t) rowsperchunk * (size_t) blocksperrow * sizeof(JBLOCK))); for (i = rowsperchunk; i > 0; i--) { result[currow++] = workspace; workspace += blocksperrow; } } return result; }
JBLOCKARRAY my_memory_mgr::alloc_barray (int pool_id, JDIMENSION blocksperrow, JDIMENSION numrows) /* Allocate a 2-D coefficient-block array */ { JBLOCKARRAY result; JBLOCKROW workspace; JDIMENSION rowsperchunk, currow, i; long ltemp; /* Calculate max # of rows allowed in one allocation chunk */ ltemp = (MAX_ALLOC_CHUNK-sizeof(large_pool_hdr)) / ((long) blocksperrow * sizeof(JBLOCK)); if (ltemp <= 0) cinfo->ERREXIT(JERR_WIDTH_OVERFLOW); if (ltemp < (long) numrows) rowsperchunk = (JDIMENSION) ltemp; else rowsperchunk = numrows; last_rowsperchunk = rowsperchunk; /* Get space for row pointers (small object) */ result = (JBLOCKARRAY) alloc_small(pool_id, (size_t) (numrows * sizeof(JBLOCKROW))); /* Get the rows themselves (large objects) */ currow = 0; while (currow < numrows) { rowsperchunk = MIN(rowsperchunk, numrows - currow); workspace = (JBLOCKROW) alloc_large(pool_id, (size_t) ((size_t) rowsperchunk * (size_t) blocksperrow * sizeof(JBLOCK))); for (i = rowsperchunk; i > 0; i--) { result[currow++] = workspace; workspace += blocksperrow; } } return result; }
void * npr_mempool_alloc_align_stat(struct npr_mempool *pool, unsigned int align_shift, unsigned int size MT_ARG ) { uintptr_t ptr, ptr_aligned; int ptr_diff, remain; unsigned char *alloc_ptr; unsigned int alloc_size; unsigned int align; uintptr_t align_mask; unsigned int aligned_size; if (size >= 1024) { return alloc_large(pool, size PASS_MT); } align = 1<<align_shift; align_mask = ~((uintptr_t)(1<<align_shift) - 1); aligned_size = (size + (align-1))&align_mask; retry: ptr = (uintptr_t)(pool->data_entry[pool->entry_index] + pool->entry_byte_pos); ptr_aligned = (ptr+(align-1))&align_mask; ptr_diff = ptr_aligned - ptr; remain = pool->entry_byte_remain - ptr_diff; alloc_ptr = (unsigned char*)ptr_aligned; if ((int)size > remain) { unsigned int next_entry_index; INC_STAT(unused_mem, pool->entry_byte_remain); /* realloc entry */ next_entry_index = pool->entry_index + 1; alloc_size = pool->entry_byte_size*2; if ((size+align) > alloc_size) alloc_size = (size+align)*2; if (next_entry_index >= pool->entry_num) { /* realloc entry pointer */ int next_entry_num = pool->entry_num * 2; pool->data_entry = (unsigned char**)realloc(pool->data_entry, next_entry_num * sizeof(unsigned char*)); pool->entry_num = next_entry_num; } pool->entry_byte_remain = pool->entry_byte_size = alloc_size; pool->entry_index = next_entry_index; pool->entry_byte_pos = 0; pool->data_entry[next_entry_index] = (unsigned char*)malloc(alloc_size); goto retry; } INC_STAT(align_padding, ptr_diff); alloc_size = (ptr_diff + aligned_size); remain -= alloc_size; pool->entry_byte_remain = remain; pool->entry_byte_pos += alloc_size; pool->alloc_small += alloc_size; INC_STAT(alloc_total, alloc_size); INC_STAT(size_each_type[mt], alloc_size); /* memset(alloc_ptr, 0x33, size); */ return alloc_ptr; }