0

I am writing a small program which prints the time it took to allocate the memory. I want to free the memory later so I want to save it in a array, but since I can loop it as many times as I want I want to make a Dynamic array to store all the addresses from the allocated Memory. This is my init code:

static __init int init_kmalloc(void)
{
    int size = sizeof(char*);
    char *buffer = kmalloc_array(loop_cnt, size, GFP_KERNEL);
    unsigned int i = 0;

    printk(KERN_DEBUG "Allocating %d times the memory size of %d\n", loop_cnt, alloc_size);
    while(i < loop_cnt)
    {
        unsigned long long start;
        unsigned long long stop;

        start = get_rdtsc();
        buffer[i] = kmalloc(alloc_size, GFP_KERNEL);
        stop = get_rdtsc();

        printk(KERN_DEBUG "%i: It took %lld ticks to allocate the memory\n", i, stop - start);
        i++;
    }

    while(i > 0)
    {
        kfree(buffer[i]);
        printk(KERN_DEBUG "Cleared\n");
        i--;
    }

    return 0;
}

I Always get these errors: Errors

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Chrissi
  • 15
  • 8
  • 3
    [Please do not post images of texts (the error message here) because they are hard to use.](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question) Textsshould be posted directly **as text** in your question. – MikeCAT May 06 '21 at 15:03

1 Answers1

1

What is wrong is that you choose char as the element of the array by selecting char* for the type of buffer. The elements of the array should pointers, so buffer should be a pointer to pointers like this (for example):

char **buffer = kmalloc_array(loop_cnt, size, GFP_KERNEL);

Use two *s, not one.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70