On Wed, May 05, 2004 at 01:55:24 +0900, Tetsuo Handa wrote: > I see. I will try to use linked list. > The 'const char *' data is a filename with caninicalized pathname, > and I don't want the array not to appear the same filename in > multiple indexes. So I will try something like this. struct filename { list_head list; const char fname[1]; } > int len = strlen(filename) + 1; int len = strlen(filename); > const char *cp = kmalloc(len, GFP_KERNEL); struct filename *cp = kmalloc(sizeof(struct filename) + len, GFP_KERNEL); > if (!cp) return; > memmove(cp, filename, len); list_init(&filename->list_head); memmove(cp, filename->fname, len + 1); > spin_lock(); > // Scan linked list. > // Add cp to linked list if not found, kfree(cp) if found. > spin_unlock(); The linked lists in kernel (in linux/list.h) are INTERNAL linked lists. You need to add struct list_head as part of your data. > > 2) Be sure to remember how kmalloc works. It can only alocate power-of-2 > > sized buffers, 32 bytes minimum, PAGE_SIZE << 5 (or something like > I don't remember, but I thought kmalloc() with less than 32 bytes > doesn't return NULL. I called kmalloc() with 6 bytes > ("const char *" + "unsigned short int") and (I thought) succeeded. > Max size I could allocate with kmalloc() is 128 * 1024 bytes, > which is equals to PAGE_SIZE << 5. > # I have no linux PC here to verify now. kmalloc allocates from generic slab caches. These exist in sizes of power-of-2, from 32 up to PAGE_SIZE << 5. When you ask some amout of memory from kmalloc, it finds the cache with smallest larger chunks and allocates one chunk from it. ------------------------------------------------------------------------------- Jan 'Bulb' Hudec <bulb@ucw.cz>
Attachment:
signature.asc
Description: Digital signature