void *realloc(void *ptr, size_t size) { size_t s; char *result; init(); dprintf("Reallocation de la zone en %lx\n", (unsigned long) ptr); if (!ptr) { dprintf(" Realloc of NULL pointer\n"); return mem_alloc(size); } if (mem_get_size(ptr) >= size) { dprintf(" Useless realloc\n"); return ptr; } result = mem_alloc(size); if (!result) { dprintf(" Realloc FAILED\n"); return NULL; } for (s = 0; s<mem_get_size(ptr); s++) result[s] = ((char *) ptr)[s]; mem_free(ptr); dprintf(" Realloc ok\n"); return result; }
/* Itérateur sur le contenu de l'allocateur */ void mem_show(void (*print)(void *b, size_t t, int free)){ struct ab* ab = headAllocatedB; struct fb* fb = headFreeB; while (fb!=NULL){ print((char*)fb+sizeof(struct fb),mem_get_size((char*)fb+sizeof(struct fb)),FREE_BLOCK); fb=fb->next; } while (ab!=NULL){ print((char*)ab+sizeof(struct ab),mem_get_size((char*)ab+sizeof(struct ab)),ALLOCATED_BLOCK); ab=ab->next; } }
bool glxx_buffer_values_are_less_than(GLXX_BUFFER_T *buffer, int offset, int count, int size, int value) { int bsize = mem_get_size(buffer->pool[buffer->current_item].mh_storage); vcos_assert(size == 1 || size == 2); vcos_assert(offset >= 0 && size >= 0 && offset + size * count <= bsize); vcos_assert(!(offset & (size - 1))); if (buffer->size_used_for_max != (uint32_t)size) { void *data = mem_lock(buffer->pool[buffer->current_item].mh_storage); // Recalculate max buffer->max = find_max(bsize / size, size, data); buffer->size_used_for_max = size; mem_unlock(buffer->pool[buffer->current_item].mh_storage); } if (buffer->max < value) return true; else { void *data = mem_lock(buffer->pool[buffer->current_item].mh_storage); int max = find_max(count, size, (uint8_t *)data + offset); mem_unlock(buffer->pool[buffer->current_item].mh_storage); return max < value; } }
static void glxx_buffer_inner_subdata(GLXX_BUFFER_INNER_T *item, int32_t offset, int32_t size, const void *data) { vcos_assert(offset >= 0 && size >= 0 && (uint32_t)offset + (uint32_t)size <= mem_get_size(item->mh_storage)); vcos_assert(data); khrn_interlock_write_immediate(&item->interlock); if(size>0) { memcpy((uint8_t *)mem_lock(item->mh_storage) + offset, data, size); mem_unlock(item->mh_storage); } }
static bool glxx_buffer_inner_data(GLXX_BUFFER_INNER_T *item, int32_t size, const void *data, bool is_new_item) { uint32_t current_size; vcos_assert(size >= 0); khrn_interlock_write_immediate(&item->interlock); current_size = mem_get_size(item->mh_storage); if (current_size != (uint32_t)size) { #ifdef __VIDEOCORE4__ MEM_HANDLE_T handle; MEM_FLAG_T flags = MEM_FLAG_DIRECT | MEM_FLAG_DISCARDABLE; if(!is_new_item) { /* unretain existing, retain new */ if(item->mh_storage!=MEM_ZERO_SIZE_HANDLE) mem_unretain(item->mh_storage); flags |= MEM_FLAG_RETAINED; } /* discardable so can be reclaimed if short of memory and no longer retained */ handle = mem_alloc_ex((uint32_t)size, 4, flags, "GLXX_BUFFER_INNER_T.storage", MEM_COMPACT_DISCARD); #else MEM_HANDLE_T handle = mem_alloc_ex((uint32_t)size, 4, MEM_FLAG_NONE, "GLXX_BUFFER_INNER_T.storage", MEM_COMPACT_DISCARD); // check, no term #endif if (handle == MEM_INVALID_HANDLE) { MEM_ASSIGN(item->mh_storage, MEM_ZERO_SIZE_HANDLE); return false; } MEM_ASSIGN(item->mh_storage, handle); mem_release(handle); } /* at this point buffer->mh_storage is guaranteed to have size size */ if (data) { memcpy(mem_lock(item->mh_storage), data, size); mem_unlock(item->mh_storage); } return true; }
void glxx_buffer_subdata(GLXX_BUFFER_T *buffer, int32_t offset, int32_t size, const void *data) { vcos_assert(offset >= 0 && size >= 0); vcos_assert(data); //if write_would_block(buffer->pool[buffer->current_item] //we pick one of the other pool items, copy the entirety of the current item //into that item, and then the new subdata if(write_would_block(&buffer->pool[buffer->current_item])) { //pick a non busy entry from the pool; uint32_t i; for(i = 0; i< GLXX_BUFFER_POOL_SIZE; i++) { if(i!=(uint32_t)buffer->current_item && !write_would_block(&buffer->pool[i])) break; } if(i<GLXX_BUFFER_POOL_SIZE) { //found one //copy existing data into this new item assuming alloc succeeds uint32_t existing_size = mem_get_size(buffer->pool[buffer->current_item].mh_storage); bool ok; void * existing_data = mem_lock(buffer->pool[buffer->current_item].mh_storage); ok = glxx_buffer_inner_data(&buffer->pool[i],existing_size,existing_data,true); mem_unlock(buffer->pool[buffer->current_item].mh_storage); if(ok) { #ifdef __VIDEOCORE4__ //unretain current one, so when fixer also unretains, retain count will fall to 0 mem_unretain(buffer->pool[buffer->current_item].mh_storage); //retain new one if(buffer->pool[i].mh_storage!=MEM_ZERO_SIZE_HANDLE) mem_retain(buffer->pool[i].mh_storage); #endif buffer->current_item = i; } } //else stick with the existing and wait } glxx_buffer_inner_subdata(&buffer->pool[buffer->current_item],offset,size,data); #ifdef REQUIRE_MAX_INDEX_CHECK buffer->size_used_for_max = 0; #endif }
static void khrn_image_to_native_buffer(KHRN_IMAGE_T *image, android_native_buffer_t *buffer, void *bits) { MEM_HANDLE_T mem_handle; uint8_t *ptr; int size; mem_handle = image->mh_storage; ptr = (uint8_t*)mem_lock(mem_handle); size = mem_get_size(mem_handle); // memcpy(bits, ptr, size); { /* V3D Y axis calibrarion and that of Android is opposite, so copying from top of src to bottom of dest */ /* This could be a place where we can look for optimization */ int image_height, image_stride; int buffer_height, buffer_stride; int copy_height, copy_stride, i; image_height = (uint16_t)(khrn_image_is_packed_mask(image->format) ? khrn_image_get_packed_mask_height(image->height) : image->height); image_stride = image->stride; buffer_height = buffer->height; buffer_stride = buffer->width * bpp; copy_height = (image_height <= buffer_height) ? image_height : buffer_height; copy_stride = (image_stride <= buffer_stride) ? image_stride : buffer_stride; #ifndef BCG_FB_LAYOUT ptr += (image->offset + size - image_stride); #endif for (i=0; i< copy_height; i++) { memcpy(bits, ptr, copy_stride); bits = (void *)((char *)bits + buffer_stride); #ifndef BCG_FB_LAYOUT ptr -= image_stride; #else ptr += image_stride; #endif } } }