Beispiel #1
0
void* mymalloc(char* file, int line, size_t size)
{
	storageElement* s = malloc(sizeof(storageElement));

	s->file = malloc(strlen(file)+1);
	strcpy(s->file, file);
	s->line = line;
	s->ptr = malloc(size);
	s->size = size;
	//Log(LOG_DEBUG, "Allocating %d bytes in heap at file %s line %d ptr %p\n", size, file, line, s->ptr);
	ListAppendNoMalloc(&heap, s, malloc(sizeof(ListElement)), sizeof(ListElement));
	state.current_size += size;
	if (state.current_size > state.max_size)
		state.max_size = state.current_size;
	return s->ptr;
}
Beispiel #2
0
/**
 * Insert an item to a list at a specific position.
 * @param aList the list to which the item is to be added
 * @param content the list item content itself
 * @param size the size of the element
 * @param index the position in the list. If NULL, this function is equivalent
 * to ListAppend.
 */
void ListInsert(List* aList, void* content, size_t size, ListElement* index)
{
	ListElement* newel = malloc(sizeof(ListElement));

	if ( index == NULL )
		ListAppendNoMalloc(aList, content, newel, size);
	else
	{
		newel->content = content;
		newel->next = index;
		newel->prev = index->prev;

		index->prev = newel;
		if ( newel->prev != NULL )
			newel->prev->next = newel;
		else
			aList->first = newel;

		++(aList->count);
		aList->size += size;
	}
}
Beispiel #3
0
/**
 * Allocates a block of memory.  A direct replacement for malloc, but keeps track of items
 * allocated in a list, so that free can check that a item is being freed correctly and that
 * we can check that all memory is freed at shutdown.
 * @param file use the __FILE__ macro to indicate which file this item was allocated in
 * @param line use the __LINE__ macro to indicate which line this item was allocated at
 * @param size the size of the item to be allocated
 * @return pointer to the allocated item, or NULL if there was an error
 */
void* mymalloc(char* file, int line, size_t size)
{
	ListElement* e = NULL;
	storageElement* s = NULL;
	static char* errmsg = "Memory allocation error";

	if ((s = malloc(sizeof(storageElement))) == NULL)
	{
		Log(LOG_ERROR, -1, errmsg);
		return NULL;
	}
	if ((s->file = malloc(strlen(file)+1)) == NULL)
	{
		Log(LOG_ERROR, -1, errmsg);
		free(s);
		return NULL;
	}
	strcpy(s->file, file);
	s->line = line;
	if ((s->ptr = malloc(size)) == NULL)
	{
		Log(LOG_ERROR, -1, errmsg);
		free(s->file);
		free(s);
		return NULL;
	}
	s->size = size;
	s->stack = NULL;
	if (trace_settings.trace_level == TRACE_MAXIMUM)
	{
		if ((s->stack = StackTrace_get(Thread_getid())) == NULL)
		{
			Log(LOG_ERROR, -1, errmsg);
			free(s->ptr);
			free(s->file);
			free(s);
			return NULL;
		}
		else
		{
			Log(TRACE_MAX, -1, "Allocating %d bytes in heap at line %d of file %s, ptr %p, heap use now %d bytes",
													 s->size, line, file, s->ptr, state.current_size);
			Log(TRACE_MAX, -1, "Stack trace is %s", s->stack);
		}
	}
	if ((e = malloc(sizeof(ListElement))) == NULL)
	{
		Log(LOG_ERROR, -1, errmsg);
		if (s->stack)
			free(s->stack);
		free(s->ptr);
		free(s->file);
		free(s);
		return NULL;
	}
	ListAppendNoMalloc(&heap, s, e, sizeof(ListElement));
	state.current_size += size;
	if (state.current_size > state.max_size)
		state.max_size = state.current_size;
	return s->ptr;
}
Beispiel #4
0
/**
 * Append an item to a list.
 * @param aList the list to which the item is to be added
 * @param content the list item content itself
 * @param size the size of the element
 */
void ListAppend(List* aList, void* content, size_t size)
{
	ListElement* newel = malloc(sizeof(ListElement));
	ListAppendNoMalloc(aList, content, newel, size);
}