简体   繁体   中英

how to get type of memory that allocated with malloc function?

I want to write an overloaded version of malloc that save size ,type ,pointer of allocated memory in the structure. if i have one structure like this :

  struct memEvent {
  char type ;
  void * ptr ;
  size_t size ;
  } ;

if i define overloaded version of malloc like this :

#define  malloc(size)    xmalloc(size,type)

and implement xmalloc like this :

void *xmalloc(size_t _size, char type) {
void *ptr1 = malloc(_size);
memEvent *newElem = (memEvent*)malloc(sizeof(memEvent));
newElem->type =??????;
newElem->ptr = &ptr1;
newElem->size = _size;
return ptr1;} 

so how can i get type of memory from standard malloc use in the code that should be like this :

  ptr = (cast-type*) malloc(byte-size)

for example : ptr = (int*) malloc(100 * sizeof(int));

One thing you could do is allocate the header and the data in a single block and return a pointer just past the header. You need to ensure that the returned pointer is suitably aligned for objects of any normal type.

The returned pointer cannot be passed directly to free() or realloc() because it is prefixed with the header, so suitable replacements are required for those functions.

This is illustrated by the example below:

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

typedef struct memEvent {
    struct {
        char type;
        size_t size;
    } _Alignas(max_align_t);
} memEvent_t;

memEvent_t *me_get_memEvent(void *p)
{
    return p != NULL ? (memEvent_t *)p - 1 : NULL;
}

char me_get_type(void *p)
{
    memEvent_t *m = me_get_memEvent(p);

    return m != NULL ? m->type : 0;
}

void me_set_type(void *p, char type)
{
    memEvent_t *m = me_get_memEvent(p);

    if (m != NULL) {
        m->type = type;
    }
}

size_t me_get_size(void *p)
{
    memEvent_t *m = me_get_memEvent(p);

    return m != NULL ? m->size : 0;
}

void *me_realloc_set_type(void *p, size_t size, char type)
{
    memEvent_t *m;

    if (size > SIZE_MAX - sizeof(*m)) {
        return NULL;
    }
    m = realloc(me_get_memEvent(p), size + sizeof(*m));
    if (m != NULL) {
        m->type = type;
        m->size = size;
        return m + 1;
    } else {
        return NULL;
    }
}

void *me_realloc(void *p, size_t size)
{
    return me_realloc_set_type(p, size, me_get_type(p));
}

void *me_alloc(size_t size, char type)
{
    return me_realloc_set_type(NULL, size, type);
}

void me_free(void *p)
{
    free(me_get_memEvent(p));
}

static void show_me(void *p)
{
    memEvent_t *m = me_get_memEvent(p);

    if (m != NULL) {
        printf("%p %zu %c\n", p, m->size, m->type);
    } else {
        printf("(null)\n");
    }
}

int main(void)
{
    void *p;
    size_t size;
    char type;

    printf("memEvent_t size %zu, alignment %zu\n",
           sizeof(memEvent_t), _Alignof(memEvent_t));

    size = 100;
    type = 'a';
    printf("* Allocating size %zu, type %c:\n", size, type);
    p = me_alloc(size, type);
    show_me(p);

    size = 200;
    printf("* Reallocating to size %zu:\n", size);
    p = me_realloc(p, size);
    show_me(p);

    type = 'b';
    printf("* Changing type to %c:\n", type);
    me_set_type(p, 'b');
    show_me(p);

    size = 300;
    type = 'c';
    printf("* Reallocating to size %zu and setting type to %c:\n",
           size, type);
    p = me_realloc_set_type(p, size, type);
    show_me(p);

    printf("* Freeing ...\n");
    me_free(p);

    printf("* Done\n");
}

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