0

I'm re-coding the malloc function using brk, sbrk & getpagesize()

I must follow two rules:

1) I must align my memory on a power of 2

It means: If the call to malloc is : malloc(9); i must return them a block of 16 byte. ( the nearest power of 2);

2) I must align the break (program end data segment) on a multiple of 2 pages.

I'm thinking about the rules, i'm wondering if i'm true;

Rule 1) I just need to make the return of my malloc (so the adress returned by malloc in hexa) a multiple of 2 ?

And for the Rule 2)

the break is the last adress in the heap if i'm not wrong, do i need to set my break like this (the break - the heap start) % (2 * getpagesize())== 0? or just the break % (2 * getpagesize() == 0? Thanks

  • 1
    “Align memory on a power of 2” is not a complete specification. One is a power of two (two to the power of zero), so any integer is a multiple of that power of two, so every address is aligned on **a** power of two. Is the specification that the memory provided by your `malloc` must be aligned on a **specific** power of two? Is that power fixed in the problem statement, is it two times the page size, is it passed by the caller to `malloc` somehow, or is it something else? – Eric Postpischil Feb 10 '20 at 01:30
  • No there's no constraint or more definition of the problem besides; Your allocation strategy must be like follow: you must align your memory on a power of 2 and the break must be aligned on a multiple of 2 pages) @EricPostpischil – freeinternet Feb 10 '20 at 01:37
  • And use the best fit algorithm (but i already figured out what it is and how i will do it) so for you the rule 1 signify nothing and i’m good no taking care of it? @EricPostpischil – freeinternet Feb 10 '20 at 01:48
  • Sorry, but teachers simply do not give problem statements like that unless they are making a mistake. As I have written, it makes no sense for the requirement for `malloc` to be merely that the memory is aligned on “a power of two”, because every memory address is aligned on a power of two. If that is the exact wording of the problem statement, you should go back to the teacher for clarification. If it is not, you should enter the exact wording in the question. – Eric Postpischil Feb 10 '20 at 02:05
  • @EricPostpischil thanks i’ll get back on the teachers for this one, and for the second rule, the break must be aligned on a multiple of 2 pages, i think the last adress in heap % 2 * getpagesize must be equal 0, am’i right? – freeinternet Feb 10 '20 at 02:12
  • Yes, the remainder of heap modulo twice the page size must be zero. However, you would not express it in code as `heap % 2 * pagesize` because `%` and `*` have the same precedence, so that would be parsed as `(heap % 2) * pagesize`. You need `heap % (2 * pagesize)` to be zero. – Eric Postpischil Feb 10 '20 at 02:22
  • @EricPostpischil lets sate power of 2 exponent 2 as the teacher wont give me anymore infos – freeinternet Feb 10 '20 at 10:59
  • You should update this question with the **exact complete text** of the problem assignment. – Eric Postpischil Feb 10 '20 at 16:17

1 Answers1

0

1) I must align my memory on a power of 2

Rule 1) I just need to make the return of my malloc (so the adress returned by malloc in hexa) a multiple of 2 ?

For an address to be aligned on power of 2 that is 2p, the address must be a multiple of 2p.

2) I must align the break (program end data segment) on a multiple of 2 pages.

the break is the last adress in the heap if i'm not wrong, do i need to set my break like this (the break - the heap start) % (2 * getpagesize())== 0? or just the break % (2 * getpagesize() == 0?

The phrase “set my break” is unclear. You need to use sbrk(0) to get the current value of the break and calculate how much you need to add to it to make it a multiple of twice the page size. That tells you where you need to start a block of memory that is aligned to a multiple of twice the page size. Then you need additional memory to contain whatever amount of data you want to put there (the amount being allocated).

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • So for the rule n1 it's like this: if they call malloc(9) i must return them a 16 byte block, i' must return the first power of 2 that can contain the malloc asked – freeinternet Feb 10 '20 at 16:12
  • @freeinternet: No. First, you still do not have a proper specification for what power of 2 must be used. The teacher’s specification, as you have reported it, is inadequate. When `malloc(9)` is called, there is some alignment requirement that we do not know, due to the inadequate specification. Let’s set it is A. What you must return is an address p such that p is a multiple of A and the bytes p through p+8 are available for the caller to use. You can do anything you want with the bytes p+9 through p+infinity; they are none of the caller’s business. – Eric Postpischil Feb 10 '20 at 16:16
  • @freeinternet: If you want to organize your `malloc` implementation into 16-byte blocks because that is convenient for you, fine. But that is an implementation choice, not a requirement of the specification as shown. – Eric Postpischil Feb 10 '20 at 16:16