I'm trying to implement ownmalloc() by calling original malloc() only once at beginning of the code.

Using this memory returned by original malloc(), each ownmalloc() call from user program uses this memory for allocation.

There are 2 lists maintained allocated and unallocated.

ownfree() function removes the allocated memory and adds back to unallocated blocks, orders them and makes the contiguous address blocks as a single block

I have two questions:

  1. How can I stop user from using unauthorized memory at run time, e.g. user asked 10 blocks and using 13 blocks to write the data.

  2. ownmalloc() calls a function(say x) which in turn calls original malloc() and takes block of memory which will be used by ownmalloc() multiple times. How can I make sure that ownmalloc() calls this function x() only once (during the first call) and at later calls, function call to x() shouldn't be done.

I tried it with this approach, but its not working in c

typedef struct mem{
    int sz;
    struct mem *next;
}node;

node *block;
static bool init=false;

void* x(){
    //call to original malloc
}

void* ownmalloc(size){
    if(!init){
        block=x();
        init=true;
    }
}

Whenever I try to make call to this ownmalloc() x() function is being called every time. How do I make it call only once?

1 Answers

0
Yoni Newman On

Try get the input from the user into a static allocated data structure and check it's size.

if the size is smaller than your allocated memory assign the input and otherwise give an error. its missing a big point of dynamic memory allocation but i think it should work.