Main Page | Modules | Class List | File List | Class Members | File Members | Related Pages

slab.c

Go to the documentation of this file.
00001 /* $Id: slab.c,v 1.9 2004/05/12 10:02:57 ch12 Exp $ */
00002 /*****************************************************************************/
00012 /* (c) 2003 Technische Universitaet Dresden
00013  * This file is part of DROPS, which is distributed under the terms of the
00014  * GNU General Public License 2. Please see the COPYING file for details.
00015  */
00016 
00031 /* L4 */
00032 #include <l4/slab/slab.h>
00033 #include <l4/log/l4log.h>
00034 #include <l4/dm_mem/dm_mem.h>
00035 
00036 #include <l4/dde_linux/dde.h>
00037 
00038 /* Linux */
00039 #include <linux/slab.h>
00040 
00041 /* local */
00042 #include "__config.h"
00043 #include "internal.h"
00044 
00045 #define CACHE_NAMELEN 20  
00048 struct kmem_cache_s
00049 {
00050   l4slab_cache_t *l4slab_cache;
00051 
00052   /* constructor func */
00053   void (*ctor)(void *, kmem_cache_t *, unsigned long);
00054   /* de-constructor func */
00055   void (*dtor)(void *, kmem_cache_t *, unsigned long);
00056 
00057   char name[CACHE_NAMELEN];
00058 };
00059 
00063 static void * alloc_grow (l4slab_cache_t * cache, void **data)
00064 {
00065   void *memp;
00066   kmem_cache_t *kcache = (kmem_cache_t *)l4slab_get_data(cache);
00067 
00068   LOGd_Enter(DEBUG_SLAB, "(name=%s)", kcache->name);
00069 
00070   if (!(memp = l4dm_mem_allocate (L4_PAGESIZE, L4DM_PINNED | L4RM_MAP)))
00071     {
00072       Panic ("dde: kmem_caches can't grow");
00073     }
00074 
00075   return memp;
00076 }
00077 
00078 #if 0
00079 void alloc_release(l4slab_cache_t *cache, void *page, void *data)
00080 {
00081 //printf("alloc_release\n");
00082  l4dm_mem_release(page);
00083 }
00084 #endif
00085 
00095 kmem_cache_t * kmem_cache_create (const char *name, size_t size,
00096                                   size_t offset, unsigned long flags,
00097                                   void (*ctor) (void *, kmem_cache_t *, unsigned long),
00098                                   void (*dtor) (void *, kmem_cache_t *, unsigned long))
00099 {
00100   kmem_cache_t *kcache;
00101 
00102   LOGd_Enter(DEBUG_SLAB, "(name=%s)", name);
00103 
00104   if (!name)
00105     {
00106       LOG_Error ("kmem_cache name required");
00107       return NULL;
00108     }
00109   if(dtor){
00110       LOG_Error("No destructors supported!");
00111       return 0;
00112   }
00113 
00114   kcache = vmalloc (sizeof (kmem_cache_t));
00115   kcache->l4slab_cache = vmalloc (sizeof (l4slab_cache_t));
00116   if (l4slab_cache_init (kcache->l4slab_cache, size, 0, alloc_grow, NULL))
00117     {
00118       LOG_Error ("Couldn't get l4slab_cache");
00119       return NULL;
00120     }
00121 
00122   l4slab_set_data(kcache->l4slab_cache, (void *)kcache);
00123   strncpy (kcache->name, name, CACHE_NAMELEN);
00124 
00125   kcache->ctor = ctor;
00126   kcache->dtor = dtor;
00127 
00128   return kcache;
00129 }
00130 
00133 int kmem_cache_destroy (kmem_cache_t * kcache)
00134 {
00135   LOGd_Enter(DEBUG_SLAB, "(name=%s)", kcache->name);
00136 
00137   l4slab_destroy (kcache->l4slab_cache);
00138   vfree (kcache->l4slab_cache);
00139   vfree (kcache);
00140 
00141   return 0;  // l4slab_destroy never fails
00142 }
00143 
00146 void * kmem_cache_alloc (kmem_cache_t * kcache, int flags)
00147 {
00148   void *p = l4slab_alloc (kcache->l4slab_cache);
00149 
00150   if (kcache->ctor)
00151     kcache->ctor(p, kcache, flags);
00152 
00153   return p;
00154 }
00155 
00158 void kmem_cache_free (kmem_cache_t * kcache, void *objp)
00159 {
00160   l4slab_free (kcache->l4slab_cache, objp);
00161 }

Linux DDE, written by Christian Helmuth  © 2003 Technische Universitaet Dresden