/** ************************************************************************** ** @brief Allocate and initialize a new queue on the stack ** @param depth The depth of the queue expressed in the number of pointers. ** @return A pointer to the new queue or NULL if allocation failed. *****************************************************************************/ caribou_queue_t* caribou_queue_new(int depth) { caribou_queue_t* queue=(caribou_queue_t*)bitmap_heap_malloc(sizeof(caribou_queue_t)); if(queue) { if ( depth == QUEUE_DEPTH_DYNAMIC ) { queue->msgs=(caribou_queue_msg_t**)NULL; queue->depth = depth; queue->count=0; } else { queue->msgs=(caribou_queue_msg_t**)bitmap_heap_malloc(depth*sizeof(caribou_queue_msg_t*)); if ( queue->msgs ) { memset(&queue->msgs[0],0,depth*sizeof(caribou_queue_msg_t*)); queue->depth = depth; queue->count=0; } else { free(queue); queue=NULL; } } } return queue; }
/** ** @brief The calloc() function allocates memory for an array of nmemb elements of size bytes each and returns a pointer to the ** allocated memory. The memory is set to zero. If nmemb or size is 0, then calloc() returns either NULL, or a unique ** pointer value that can later be successfully passed to free(). */ extern void* bitmap_heap_calloc(size_t nmemb, size_t size) { size_t bytes = nmemb*size; void* pointer = bitmap_heap_malloc(bytes); if ( pointer ) memset(pointer,0,bytes); return pointer; }
/** * @brief Allocate a new bytequeue struct and initialize it. * @param size The size of the queue storage in bytes * @return A pointer to the new caribou_bytequeue_t struct. */ caribou_bytequeue_t* caribou_bytequeue_new(uint16_t size) { caribou_bytequeue_t* rc = (caribou_bytequeue_t*)bitmap_heap_malloc(sizeof(caribou_bytequeue_t)); if ( rc ) { memset(rc,0,sizeof(caribou_bytequeue_t)); rc->queue = (uint8_t*)bitmap_heap_malloc(size); if ( rc->queue ) { rc->size = size; } else { bitmap_heap_free(rc); rc=NULL; } } return rc; }
/** ** realloc() changes the size of the memory block pointed to by ptr to size bytes. The contents will be unchanged to the minimum ** of the old and new sizes; newly allocated memory will be uninitialized. If ptr is NULL, then the call is equivalent to mal‐ ** loc(size), for all values of size; if size is equal to zero, and ptr is not NULL, then the call is equivalent to free(ptr). ** Unless ptr is NULL, it must have been returned by an earlier call to malloc() or realloc(). If the area pointed to ** was moved, a free(ptr) is done. */ extern void* bitmap_heap_realloc(void* pointer, size_t size) { if (pointer != NULL && size > 0 ) { int32_t blocks = to_blocks(size); int32_t block=(-1); int32_t used; int lvl = caribou_lib_lock(); /** Search each heap... */ for(heap_num=0; heap_num < heap_count; heap_num++) { block = from_pointer(HEAP_STATE(heap_num),pointer); if ( block >= 0 ) break; } if ( block >= 0 ) { used = blocks_used(HEAP_STATE(heap_num),block); if (blocks > used) { if (!extend(HEAP_STATE(heap_num),block,used,blocks-used)) /* attempt to extend existing block */ { int32_t target; deallocate(HEAP_STATE(heap_num),block,used); /* make currently allocated blocks available to be re-allocated.. */ #if defined(CARIBOU_MPU_ENABLED) heap_state->heap_current_thread = caribou_thread_current(); #endif target = locate_free(HEAP_STATE(heap_num),blocks); /* ...then attempts to locate a sequence of free blocks... */ if (target >= 0 ) { void* pTarget = allocate(HEAP_STATE(heap_num),target,blocks); /* allocate the new blocks... */ memmove(pTarget,pointer,used*HEAP_BLOCK_SIZE); /* ...and move the data to the new area. */ pointer = pTarget; } else { /* * The re-allocation failed in the current heap pool. * It may be possible to get a fit in another pool. */ void* pTarget; if ( (pTarget = bitmap_heap_malloc(size)) != NULL ) { memmove(pTarget,pointer,used*HEAP_BLOCK_SIZE); pointer = pTarget; } else { notify_heap_invalid_realloc(pointer,size); pointer = NULL; } } } } else if (blocks < used) { /* shrink the allocation */ deallocate(HEAP_STATE(heap_num),block,used); allocate(HEAP_STATE(heap_num),block,blocks); } } else { notify_heap_invalid_realloc(pointer,size); pointer = NULL; } caribou_lib_lock_restore(lvl); } else if (pointer != NULL && size == 0) { bitmap_heap_free(pointer); pointer=NULL; } else if (pointer == NULL ) { pointer = bitmap_heap_malloc(size); } return pointer; }