简体   繁体   中英

Customized memory management with sbrk

Following is the code for a simple malloc implementation. A linked list is initiated with a head and tail pointer for memory management. Now in the function, there is only a single call implemented when the list is uninitialized, where the list's head is initialized. Once I return the underlying pointer to main , the program gives segmentation fault . On the other hand, the following test function with almost the same parameters except for the complicated handling of linked list computes correctly and displays the result. Can anyone please tell what am I missing here?

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <sys/types.h>

typedef struct Items{
  size_t size_of_object;
  size_t free;
} Items;

typedef struct Node{
  void *ptr;
  void *next;
  Items Item;
}Node ;

typedef struct LinkedList{
  Node *head;
  Node *tail;
} LinkedList;


LinkedList memory_list;

void *salmalloc(size_t size_of_object) {
  if (memory_list.head == NULL) {

    memory_list.head = sbrk(sizeof(Node));
    memory_list.head->ptr = sbrk(size_of_object);
    memory_list.head->Item.size_of_object = size_of_object;
    memory_list.tail = NULL;
    memory_list.head->next = NULL;
    memory_list.head->Item.free = 1;

    return memory_list.head->ptr;    
  }
}

void *test(size_t size) {
  void *p = sbrk(size);
  return p;
}

void main(){
  char *p = NULL;
  char a = 'B';
  p = salmalloc(sizeof(char));
  *p = a;
  printf("%c\n", *p);

}

I see a few issues:

  1. You haven't initialized memory_list .
  2. salmalloc is missing an else part and since there is no return it will return random garbage in this case.
  3. You need to check the return value of sbrk , it may fail (but salmalloc looks like work-in-progress anyway, isn't it?).
  4. You need to check the return value of salmalloc , it may fail.

Here's a version that works on my system:

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <sys/types.h>

typedef struct Items{
        size_t size_of_object;
        size_t free;
} Items;

typedef struct Node{
        void *ptr;
        void *next;
        Items Item;
}Node ;

typedef struct LinkedList{
        Node *head;
        Node *tail;
} LinkedList;


LinkedList memory_list = { 0 };

void *salmalloc(size_t size_of_object) {
        if (memory_list.head == NULL) {
                memory_list.head = sbrk(sizeof(Node));
                memory_list.head->ptr = sbrk(size_of_object);
                memory_list.head->Item.size_of_object = size_of_object;
                memory_list.tail = NULL;
                memory_list.head->next = NULL;
                memory_list.head->Item.free = 1;

                return memory_list.head->ptr;
        } else {
                return NULL;
        }
}

int main(){
        char *p = NULL;
        char a = 'B';
        p = salmalloc(sizeof(char));
        if (p == NULL) {
                printf("Allocation failed.\n");
                return 1;
        }
        *p = a;
        printf("%c\n", *p);
        return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM