コード例 #1
0
ファイル: queue.c プロジェクト: 8bitgeek/caribou-rtos
/** **************************************************************************
 ** @brief Delete a queue previously created with caribou_queue_new().
 ** @param queue A pointver to the queue to delete.
 *****************************************************************************/
void caribou_queue_delete(caribou_queue_t* queue)
{
	if ( queue )
	{
		if ( queue->msgs )
			bitmap_heap_free(queue->msgs);
		bitmap_heap_free(queue);
	}
}
コード例 #2
0
ファイル: bytequeue.c プロジェクト: 8bitgeek/caribou-rtos
/**
 * @brief Free a caribou_bytequeue_t structure, previously allocated by caribou_bytequeue_new().
 * @param queue Pointer to a previously allocated caribou_bytequeue_t structure. 
 * @return Return true if freed, else false.
 */
bool caribou_bytequeue_free(caribou_bytequeue_t* queue)
{
	bool rc=false;
	if ( queue )
	{
		int state = caribou_interrupts_disable();
		if ( queue->queue )
			bitmap_heap_free(queue->queue);
		bitmap_heap_free(queue);
		rc=true;
		caribou_interrupts_set(state);
	}
	return rc;
}
コード例 #3
0
ファイル: bytequeue.c プロジェクト: 8bitgeek/caribou-rtos
/**
 * @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;
}
コード例 #4
0
ファイル: bitmap_heap.c プロジェクト: genocidex1/blue
/**
** 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;
}