Anonymous Anonymous - 1 month ago
141 0

No description

C

Linked List Memory Pool

#include <stdio.h>
#include <stdlib.h>

struct node {
        int val;
        struct node* next;
};

typedef struct node Node;

struct mem_pool {
        Node** pool;
        int poolsize;
        int emptypos;
};

mem_pool MP;

Node* get_address() {
        return MP.pool[MP.emptypos++];
}

Node* create_list(void) {
        Node* head = get_address();
        head->next = NULL;
        return head;
}

void add_element(Node* list, int elem) {
        Node* prev = MP.pool[MP.emptypos - 1];
        Node* temp = get_address();
        printf("Addr %d\n", temp);
        temp->next = NULL;
        temp->val = elem;
        prev->next = temp;
        printf("%d\n", prev->next->val);
}

int main() {
        MP.poolsize = sizeof(Node) * 128;
        MP.pool = (Node**) malloc(sizeof(Node*) * MP.poolsize);
        MP.emptypos = 0;
        for(int i = 0; i < 128; ++i) {
                MP.pool[i] = (Node *) malloc(sizeof(Node));
                MP.pool[i]->next = NULL;
        }
        Node* FUCKINLIST = create_list();
        add_element(FUCKINLIST, 3);
        add_element(FUCKINLIST, -11);
        add_element(FUCKINLIST, 3333);
        add_element(FUCKINLIST, 4);
        add_element(FUCKINLIST, 23445);
        add_element(FUCKINLIST, 9994);
        Node* temp = FUCKINLIST;
        while(temp->next) {
                printf("%d", temp->next->val);
                if(temp->next->next)
                        printf(" -> ");
                temp = temp->next;
        }
        return 0;
}