0

I am developing a C library which has three functions. Init function takes a (void*) pointer to a memory chunk. I need to develop functions to allocate and deallocate memory blocks from that said chunk. What this means is that I have to keep track of which parts of the memory chunk I have allocated and which parts are free. Problem is, the structure I will implement to track the memory also has to be part of said memory chunk. I am not allowed to allocate new memory for my management structure.

And I have no idea how to do that.

Currently, I am planning to designate first few hundred bytes as header space and divide the rest into frames of equal size. I will use header space to create an array which will keep track of which frames are allocated. To do that, I need a way to convert memory address into long int so I can save them into the array and my search so far yielded nothing.

Is there any way to accomplish that? Failing that is there any other way to implement a management structure in this situation.

10
  • 2
    You should use integer offsets for everything within the memory chunk, not try to convert the pointers to integers. Commented Dec 11, 2019 at 21:13
  • 1
    "need a way to convert memory address into long int". You only need frame numbers if equal sized frames or offsets if not. In fact, for equal sized frames you can even save alot of space with a bit map for the free list. Commented Dec 11, 2019 at 21:14
  • 2
    Another common alternative is to put a small fixed sized header in front of each managed block and use that to chain up blocks. Commented Dec 11, 2019 at 21:17
  • 1
    All that said, this isn't really an appropriate question for SO. We're not a design discussion forum, you need to ask questions about specific code that you've written. So take the above advice and try to code something. If you can't get it to work, post what you wrote and we'll help you fix it. Commented Dec 11, 2019 at 21:17
  • 2
    @EricPostpischil My description may be a bit terse, but the point is that he needs to show something more specific that we can help correct, rather than asking for a general design. Commented Dec 11, 2019 at 21:29

1 Answer 1

1

To do that, I need a way to convert memory address into long int so I can save them into the array and my search so far yielded nothing.

Is there any way to accomplish that?

Generally, you do not need to convert memory addresses to an integer type merely to keep track of them. Options include:

  • Work with pointers within the memory chunk you are given, using char * to perform arithmetic.
  • Subtract the base address of the memory (again with char *) from pointers within it to get offsets of type ptrdiff_t (defined in <stddef.h>) and use those.
  • Convert the addresses to the integer type uintptr_t (defined in <stdint.h>). Unlike the other options, this has implementation-dependent behavior. In common C implementations, the result of conversion will be a simple memory address that you can perform arithmetic on as expected. But, in some C implementations, the result will be more complicated, so the code will not be fully portable.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.