Quantcast
Channel: Active questions tagged kernel - Stack Overflow
Viewing all articles
Browse latest Browse all 6500

Allocating memory for array of struct in linux kernel and got strange output od structs ids

$
0
0

I wrote a program in linux kernel, which should allocate memory for 5 simple structures each of with unique id, so i used static int in a constructor and increment it and in the end i just print messages to the buffer. When i saw the buffer i got a strange result because the ids had values like 0, 64, 128, 192, 256 i was surprised because i thought that i will see the values like 0, 1, 2, 3, 4. Why did i get result like this it is something wrong?

Output:

[ 2653.505140] Example struct id: 0
[ 2653.505143] Example string field content: Test
[ 2653.526565] Example struct id: 64
[ 2653.526568] Example string field content: Test
[ 2653.526623] Example struct id: 128
[ 2653.526625] Example string field content: Test
[ 2653.550439] Example struct id: 192
[ 2653.550443] Example string field content: Test
[ 2653.550513] Example struct id: 256
[ 2653.550514] Example string field content: Test

There is my code:

#include<linux/module.h>#include<linux/slab.h>#include<linux/string.h>static struct example_struct {    unsigned int id;    char example_string[10];} *example_struct_pointer[5];static struct kmem_cache *example_cachep[5];static void example_constructor(void *argument){    static unsigned int id;    static char test_string[] = "Test";    struct example_struct *example = (struct example_struct *)argument;    example->id = id;    strcpy(example->example_string,test_string);    id++;}void print_example_struct(struct example_struct *example){    pr_notice("Example struct id: %d\n",example->id);    pr_notice("Example string field content: %s\n",example->example_string);}static int __init zad1_init(void){    int i;    for(i = 0; i < 5; i++){        example_cachep[i] = kmem_cache_create("example cache", sizeof(struct example_struct),0, SLAB_HWCACHE_ALIGN|SLAB_POISON|SLAB_RED_ZONE, example_constructor);        if(IS_ERR(example_cachep[i])) {            pr_alert("Error creating cache: %ld\n",PTR_ERR(example_cachep[i]));            return -ENOMEM;        }    }    for(i = 0; i < 5; i++){        example_struct_pointer[i] = (struct example_struct *) kmem_cache_alloc(example_cachep[i],GFP_KERNEL);        if(IS_ERR(example_struct_pointer[i])) {            pr_alert("Error allocating form cache: %ld\n", PTR_ERR(example_struct_pointer[i]));            kmem_cache_destroy(example_cachep[i]);            return -ENOMEM;        }    }    return 0;}static void __exit zad1_exit(void){    int i;    for(i = 0; i < 5; i++){        if(example_cachep[i]) {            if(example_struct_pointer[i]) {                print_example_struct(example_struct_pointer[i]);                kmem_cache_free(example_cachep[i],example_struct_pointer[i]);            }            kmem_cache_destroy(example_cachep[i]);        }    }}module_init(zad1_init);module_exit(zad1_exit);MODULE_LICENSE("GPL");

Viewing all articles
Browse latest Browse all 6500

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>