简体   繁体   中英

Allocating different blocks of memory from a buffer

Beginner here. Is it possible to get a buffer, for example:

char buffer[1024];

And split it into smaller chunks of memory (random size depending on user input) using malloc until there's no more space in the buffer? For example: 1st block = 16, 2nd block = 256, 3rd block = 32, etc.. until I reach 1024. Also I would like to create a structure for every block created. I'm using plain C.

Even though I'm not sure if I can do that, I've started something:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
    int x = 0;

    printf("Enter size of block to be allocated: ");
    scanf("%d", &x);
    /*(need to implement): call the following function until there's no more
    space left in the buffer*/
    allocate(x);

    return 0;
}

void *allocate(size_t size)
{
    char buffer[1024];
    char *block;
    /*The following allocates a block with the size of the user input.
    How do I associate it with the buffer?*/
    block = (char *) malloc(size + 1);

    //Creates a structure. How do I create one for every block created?
    typedef struct blk_struct
    {
        int data;
        struct blk_struct *size_blk;
        struct blk_struct *next;
    }blk_struct;
    blk_struct *first;
}

Research I've done: Google and SO. Couldn't find anything on both. Perhaps I'm not searching for the right key words? Thanks in advance.

Malloc uses its own internal memory management, so it will not sub-allocate from memory that you provide.

There are a number of malloc implementations available (Google "malloc alternatives") that provide memory management strategies optimized for various use cases (embedded, multiprocessor, debugging). You may well find an existing solution that addresses the underlying problem you are trying to address with this question.

write your own function which will do the same as malloc because malloc has already it's own implementation so it won't take memory from your allocated buffer.

It always allocates memory from heap

You can write your own function like this :

char buffer[1024];// fixed size buffer
int freeindex; // global variable or make it static to keep track of allocated memory
char* mem_alloc(size_t size)
{
 if(freeindex == 1023 || (freeindex + size ) > 1023)
  return NULL;
 char * ret_addr = &buffer[freeindex];
 freeindex+=size;
 return ret_addr;
}

Remember for this, you will have to write mem_free() your own free() function

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

#define BUFFER_SIZE    1024

//Creates a structure. How do I create one for every block created?
typedef struct blk_struct
{
    char *dataptr;
    int start_blk, size_blk;
    struct blk_struct *prev;
    struct blk_struct *next;
}blk_struct;

char buffer[BUFFER_SIZE];

blk_struct *first = NULL;
blk_struct *last = NULL;

int main(void)
{
    int x = 0;
    int *a;
    char *b;

    printf("Enter size of block to be allocated: ");
    scanf("%d", &x);
    /*(need to implement): call the following function until there's no more
    space left in the buffer*/
    a = allocate(sizeof(int) * 10);
    b = allocate(sizeof(char) * 10);

    return 0;
}

void *allocate(size_t size)
{
    blk_struct *block;

    /* checking for required memory */
    if (((last->dataptr + last->size_blk + size) - buffer) > BUFFER_SIZE)
        return NULL; /* Memory Full */

    /*The following allocates a block with the size of the user input.
    How do I associate it with the buffer?*/
    block = malloc(sizeof(blk_struct));

    /* Changing the first and last block */
    if (first) {
        /* Filling Block Info */
        block->dataptr = buffer;
        block->start_blk = 0;
        block->size_blk = size;
        block->prev = NULL;
        block->next = NULL;

        first = block;
        last = block;
    }
    else {
        /* Filling Block Info */
        block->dataptr = last->dataptr + last->size_blk;
        block->start_blk = last->start_blk + last->size_blk;
        block->size_blk = size;
        block->prev = last;
        block->next = NULL;

        last->next = block;
        last = block;
    }

    return block->dataptr;
}

I hope this helps....,

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