示例#1
0
/* create or initialize empty slist structure */
slist *
sinit() {
	slist *sl;

	sl=(slist *)sf_calloc(1, sizeof(slist));
	if(!sl)
		return NULL;

	sl->listlen = 4;

	sl->list = (char **)sf_malloc(sizeof(char *) * sl->listlen);
	if(!sl->list) {
		free(sl);
		return NULL;
	}

	sl->lens = (size_t *)sf_malloc(sizeof(size_t) * sl->listlen);
	if(!sl->lens) {
		free(sl->list);
		free(sl);
		return NULL;
	}

	*(sl->list) = NULL;
	*(sl->lens) = 0;

	return sl;
};
示例#2
0
文件: sfmm.c 项目: phyllispeng/Malloc
/*
* Allocate an array of nmem elements each of size bytes
*/
void* sf_calloc(size_t nmemb, size_t size){
	
	size_t user_size = nmemb*size;
	void * re_malloc =sf_malloc(user_size);
	if(re_malloc != NULL){
		memset(re_malloc,0,user_size); 	
	}
    //printf("calloc");
	return re_malloc;
}
示例#3
0
void* sf_calloc(size_t nmemb, size_t size){
	if(nmemb <=0 || size <= 0){ 
		errno = ENOMEM;
		return NULL;
	}

	uintptr_t* result = (void *) sf_malloc(nmemb * size);
	uintptr_t count = get_loadSize(result - 1) / 8;
	int i;
	for(i = 0; i < count; i++)
		*(result + i) = 0;

	return (void *) result;
} 
示例#4
0
/**
  Allocate a sized block of memory.

  @param size   The size of the memory block in bytes.
  @param flags  Failure action modifiers (bitmasks).

  @return A pointer to the allocated memory block, or NULL on failure.
*/
void *my_malloc(size_t size, myf my_flags)
{
  void* point;
  DBUG_ENTER("my_malloc");
  DBUG_PRINT("my",("size: %lu  my_flags: %d", (ulong) size, my_flags));
  if (!(my_flags & (MY_WME | MY_FAE)))
    my_flags|= my_global_flags;

  /* Safety */
  if (!size)
    size=1;

  point= sf_malloc(size);
  DBUG_EXECUTE_IF("simulate_out_of_memory",
                  {
                    my_free(point);
                    point= NULL;
                  });
示例#5
0
/* Add a string to the end of the slist structure */
int
sadd2(slist *s, char *msg, size_t len) {
	char *tmp;

	if(!s || !msg)
		return -1;

	tmp = (char *)sf_malloc(len+1);
	if(!tmp)
		return -1;
	memcpy(tmp, msg, len);
	tmp[len] = 0;

	if(_sf_add_internal(s, tmp, len) == -1) {
		free(tmp);
		return -1;
	}

	return 0;
};
示例#6
0
int32_t tcp_buffer_new(struct tcp_buffer_class **tcpbuf, size_t size)
{
    struct tcp_buffer_class *cbuf;

    cbuf = sf_malloc(sizeof(*cbuf));
    if (!cbuf)
        return -1;

    memset(cbuf, 0, sizeof(*cbuf));

    ts_cbuffer_new(&cbuf->cbuffer, size);
    if (!cbuf->cbuffer) {
        sf_free(cbuf);
        return -1;
    }

    cbuf->size = size;
    *tcpbuf = cbuf;
    return 0;
}
示例#7
0
int main(int argc, char *argv[]) {
    // Initialize the custom allocator
    sf_mem_init(MAX_HEAP_SIZE);

    // Tell the user about the fields
    info("Initialized heap with %dmb of heap space.\n", MAX_HEAP_SIZE >> 20);
    //press_to_cont(); 

    // Print out title for first test
    printf("=== Test1: Allocation test ===\n");
    // Test #1: Allocate an integer
    int *value1 = sf_malloc(sizeof(int));
    null_check(value1, sizeof(int));
    payload_check(value1);
    // Print out the allocator block
    sf_varprint(value1);
    //press_to_cont();

    // Now assign a value
    printf("=== Test2: Assignment test ===\n");
    info("Attempting to assign value1 = %d\n", VALUE1_VALUE);
    // Assign the value
    *value1 = VALUE1_VALUE;
    // Now check its value
    check_prim_contents(value1, VALUE1_VALUE, "%d", "value1");
    //press_to_cont();

    printf("=== Test3: Allocate a second variable ===\n");
    info("Attempting to assign value2 = %ld\n", VALUE2_VALUE);
    long *value2 = sf_malloc(sizeof(long));
    null_check(value2, sizeof(long));
    payload_check(value2);
    sf_varprint(value2);
    // Assign a value
    *value2 = VALUE2_VALUE;
    // Check value
    check_prim_contents(value2, VALUE2_VALUE, "%ld", "value2");
    //press_to_cont();

    printf("=== Test4: does value1 still equal %d ===\n", VALUE1_VALUE);
    check_prim_contents(value1, VALUE1_VALUE, "%d", "value1");
    //press_to_cont();

    // Snapshot the freelist
    printf("=== Test5: Perform a snapshot ===\n");
    sf_snapshot(true);
    //press_to_cont();

    // Free a variable
    printf("=== Test6: Free a block and snapshot ===\n");
    info("Freeing value1...\n");
    sf_free(value1);
    sf_snapshot(true);
    //press_to_cont();

    // Allocate more memory
    printf("=== Test7: 8192 byte allocation ===\n");
    void *memory = sf_calloc(4096,1);
    sf_varprint(memory);
    perror("Testing calloc");
    sf_snapshot(true);
    memory = sf_malloc(8192);
    sf_varprint(memory);
    perror("Testing free");
    sf_free(memory);
    //press_to_cont();

    return EXIT_SUCCESS;
}
示例#8
0
int main()
{
	char* buffer = sf_malloc(1024);
	puts("Hello.");
	sf_free(buffer);

	sf_list_ptr my_list = sf_list_alloc();

	// Test single node list

	sf_list_node_ptr node1 = sf_list_node_alloc(NULL);
	sf_list_push(my_list, node1);
	assert(my_list->first == node1);
	assert(my_list->last == node1);
	assert(node1->next == NULL);
	assert(node1->prev == NULL);

	sf_list_node_ptr popped_node = sf_list_pop(my_list);
	assert(popped_node == node1);
	assert(node1->next == NULL);
	assert(node1->prev == NULL);
	assert(my_list->first == NULL);
	assert(my_list->last == NULL);

	// Test two node list: push 2 nodes

	sf_list_node_ptr node2 = sf_list_node_alloc(NULL);
	sf_list_push(my_list, node1);
	sf_list_push(my_list, node2);
	assert(my_list->first == node1);
	assert(my_list->last == node2);
	assert(node1->prev == NULL);
	assert(node1->next == node2);
	assert(node2->prev == node1);
	assert(node2->next == NULL);

	// Pop node 2

	popped_node = sf_list_pop(my_list);
	assert(popped_node == node2);
	assert(node2->next == NULL);
	assert(node2->prev == NULL);
	assert(node1->next == NULL);
	assert(node1->prev == NULL);
	assert(my_list->first == node1);
	assert(my_list->last == node1);

	// Pop node 1

	popped_node = sf_list_pop(my_list);
	assert(popped_node == node1);
	assert(node1->next == NULL);
	assert(node1->prev == NULL);
	assert(my_list->first == NULL);
	assert(my_list->last == NULL);

	// Test three node list: push 3 nodes

	sf_list_node_ptr node3 = sf_list_node_alloc(NULL);
	sf_list_push(my_list, node1);
	sf_list_push(my_list, node2);
	sf_list_push(my_list, node3);
	assert(my_list->first == node1);
	assert(my_list->last == node3);
	assert(node1->prev == NULL);
	assert(node1->next == node2);
	assert(node2->prev == node1);
	assert(node2->next == node3);
	assert(node3->prev == node2);
	assert(node3->next == NULL);

	// Pop node 3

	popped_node = sf_list_pop(my_list);
	assert(popped_node == node3);
	assert(node3->next == NULL);
	assert(node3->prev == NULL);
	assert(node2->next == NULL);
	assert(node2->prev == node1);
	assert(node1->next == node2);
	assert(node1->prev == NULL);
	assert(my_list->first == node1);
	assert(my_list->last == node2);

	// Pop node 2

	popped_node = sf_list_pop(my_list);
	assert(popped_node == node2);
	assert(node2->next == NULL);
	assert(node2->prev == NULL);
	assert(node1->next == NULL);
	assert(node1->prev == NULL);
	assert(my_list->first == node1);
	assert(my_list->last == node1);

	// Pop node 1

	popped_node = sf_list_pop(my_list);
	assert(popped_node == node1);
	assert(node1->next == NULL);
	assert(node1->prev == NULL);
	assert(my_list->first == NULL);
	assert(my_list->last == NULL);

	// Test insert_after

	sf_list_insert_after(my_list, node1, NULL);
	assert(my_list->first == node1);
	assert(my_list->last == node1);
	assert(node1->next == NULL);
	assert(node1->prev == NULL);

	sf_list_insert_after(my_list, node2, node1);
	assert(my_list->first == node1);
	assert(my_list->last == node2);
	assert(node1->prev == NULL);
	assert(node1->next == node2);
	assert(node2->prev == node1);
	assert(node2->next == NULL);

	sf_list_insert_after(my_list, node3, node2);
	assert(my_list->first == node1);
	assert(my_list->last == node3);
	assert(node1->prev == NULL);
	assert(node1->next == node2);
	assert(node2->prev == node1);
	assert(node2->next == node3);
	assert(node3->prev == node2);
	assert(node3->next == NULL);

	sf_list_pop(my_list);
	sf_list_insert_after(my_list, node3, node1); // makes: 1, 3, 2
	assert(my_list->first == node1);
	assert(my_list->last == node2);
	assert(node1->prev == NULL);
	assert(node1->next == node3);
	assert(node3->prev == node1);
	assert(node3->next == node2);
	assert(node2->prev == node3);
	assert(node2->next == NULL);

	sf_list_pop(my_list);
	sf_list_pop(my_list);
	sf_list_pop(my_list);

	// Test remove

	sf_list_push(my_list, node1);
	sf_list_remove(my_list, node1);
	assert(my_list->first == NULL);
	assert(my_list->last == NULL);

	sf_list_push(my_list, node1);
	sf_list_push(my_list, node2);
	sf_list_remove(my_list, node2);
	assert(my_list->first == node1);
	assert(my_list->last == node1);
	assert(node1->next == NULL);
	assert(node1->prev == NULL);
	sf_list_pop(my_list);

	sf_list_push(my_list, node1);
	sf_list_push(my_list, node2);
	sf_list_remove(my_list, node1);
	assert(my_list->first == node2);
	assert(my_list->last == node2);
	assert(node2->next == NULL);
	assert(node2->prev == NULL);
	sf_list_pop(my_list);

	sf_list_push(my_list, node1);
	sf_list_push(my_list, node2);
	sf_list_push(my_list, node3);
	sf_list_remove(my_list, node1);
	assert(my_list->first == node2);
	assert(my_list->last == node3);
	assert(node2->next == node3);
	assert(node2->prev == NULL);
	assert(node3->next == NULL);
	assert(node3->prev == node2);
	sf_list_pop(my_list);
	sf_list_pop(my_list);

	sf_list_push(my_list, node1);
	sf_list_push(my_list, node2);
	sf_list_push(my_list, node3);
	sf_list_remove(my_list, node2);
	assert(my_list->first == node1);
	assert(my_list->last == node3);
	assert(node1->next == node3);
	assert(node1->prev == NULL);
	assert(node3->next == NULL);
	assert(node3->prev == node1);
	sf_list_pop(my_list);
	sf_list_pop(my_list);

	sf_list_push(my_list, node1);
	sf_list_push(my_list, node2);
	sf_list_push(my_list, node3);
	sf_list_remove(my_list, node3);
	assert(my_list->first == node1);
	assert(my_list->last == node2);
	assert(node1->next == node2);
	assert(node1->prev == NULL);
	assert(node2->next == NULL);
	assert(node2->prev == node1);
	sf_list_pop(my_list);
	sf_list_pop(my_list);

	// Clean up

	sf_list_node_free(node1);
	sf_list_node_free(node2);
	sf_list_node_free(node3);
	sf_list_free(my_list);

	sf_malloc_check();

	return 0;
}