Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix]fix the bugs of overmemory in buddy2_size function. #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions buddy2.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ struct buddy2 {
unsigned longest[1];
};

#define LEFT_LEAF(index) ((index) * 2 + 1)
#define RIGHT_LEAF(index) ((index) * 2 + 2)
#define PARENT(index) ( ((index) + 1) / 2 - 1)
#define LEFT_LEAF(index) ((index) * 2 + 1)
#define RIGHT_LEAF(index) ((index) * 2 + 2)
#define PARENT(index) (((index) + 1) / 2 - 1)

#define IS_POWER_OF_2(x) (!((x)&((x)-1)))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define IS_POWER_OF_2(x) (!((x)&((x)-1)))
#define MAX(a, b) ((a) > (b) ? (a) : (b))

#define ALLOC malloc
#define FREE free
Expand Down Expand Up @@ -123,8 +123,11 @@ int buddy2_size(struct buddy2* self, int offset) {
assert(self && offset >= 0 && offset < self->size);

node_size = 1;
for (index = offset + self->size - 1; self->longest[index] ; index = PARENT(index))
for (index = offset + self->size - 1; self->longest[index] ; index = PARENT(index)) {
node_size *= 2;
if (index == 0)
break;
}

return node_size;
}
Expand Down