6

i have an array

char* temp;
temp=kmalloc(3,GFP_KERNEL);

i need to expand this array each time i call this function Note: Realloc can't be used in linux kernel i dont know if it exists

4 Answers 4

8

Roll your own realloc but be noted that realloc is a poorly designed function interface. Just allocate a new buffer with kmalloc and memcpy the old data into the new buffer; that's essentially all that realloc does if it cannot expand the buffer in place.

Sign up to request clarification or add additional context in comments.

1 Comment

I wouldn't keep doing 3-byte kmallocs and memcpy. Just use kmalloc in larger chunks like 1K, then you only need to do the realloc once in a while
4

<linux/slab.h> does have krealloc() to go with kmalloc(). This was added in 2007 / kernel 2.6.22.

Comments

1

I would suggest you should use a fixed size ring buffer based upon a mmapped memory(or something of that sort) instead of expanding allocation every time.

Comments

0

You probably want to using something like Linux's Flexible Array implementation. See http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=blob;f=Documentation/flexible-arrays.txt;h=df904aec99044f8056ac530b9e9dc6de8f26f73e;hb=HEAD

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.