SimpleOS

LXR

Navigation



Site hébergé par : enix

The LXR Cross Referencer for SOS

source navigation ]
diff markup ]
identifier search ]
general search ]
 
 
Article:1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 6.5 ] [ 7 ] [ 7.5 ] [ 8 ] [ 9 ] [ 9.5 ]

001 /* Copyright (C) 2004 David Decotigny
002 
003    This program is free software; you can redistribute it and/or
004    modify it under the terms of the GNU General Public License
005    as published by the Free Software Foundation; either version 2
006    of the License, or (at your option) any later version.
007    
008    This program is distributed in the hope that it will be useful,
009    but WITHOUT ANY WARRANTY; without even the implied warranty of
010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
011    GNU General Public License for more details.
012    
013    You should have received a copy of the GNU General Public License
014    along with this program; if not, write to the Free Software
015    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
016    USA. 
017 */
018 
019 #include <sos/assert.h>
020 #include <sos/macros.h>
021 
022 #include "physmem.h"
023 #include "kmem_vmm.h"
024 #include "kmem_slab.h"
025 
026 #include "kmalloc.h"
027 
028 /* The cache structures for these caches, the object size, their
029    names, and some number of pages that contain them. They might not
030    necessarily be powers of 2s. */
031 static struct {
032   const char             *name;
033   sos_size_t             object_size;
034   sos_count_t            pages_per_slab;
035   struct sos_kslab_cache *cache;
036 } kmalloc_cache[] =
037   {
038     { "kmalloc 8B objects",     8,     1  },
039     { "kmalloc 16B objects",    16,    1  },
040     { "kmalloc 32B objects",    32,    1  },
041     { "kmalloc 64B objects",    64,    1  },
042     { "kmalloc 128B objects",   128,   1  },
043     { "kmalloc 256B objects",   256,   2  },
044     { "kmalloc 1024B objects",  1024,  2  },
045     { "kmalloc 2048B objects",  2048,  3  },
046     { "kmalloc 4096B objects",  4096,  4  },
047     { "kmalloc 8192B objects",  8192,  8  },
048     { "kmalloc 16384B objects", 16384, 12 },
049     { NULL, 0, 0, NULL }
050   };
051 
052 
053 sos_ret_t sos_kmalloc_subsystem_setup()
054 {
055   int i;
056   for (i = 0 ; kmalloc_cache[i].object_size != 0 ; i ++)
057     {
058       struct sos_kslab_cache *new_cache;
059       new_cache = sos_kmem_cache_create(kmalloc_cache[i].name,
060                                         kmalloc_cache[i].object_size,
061                                         kmalloc_cache[i].pages_per_slab,
062                                         0,
063                                         SOS_KSLAB_CREATE_MAP
064                                         );
065       SOS_ASSERT_FATAL(new_cache != NULL);
066       kmalloc_cache[i].cache = new_cache;
067     }
068   return SOS_OK;
069 }
070 
071 
072 sos_vaddr_t sos_kmalloc(sos_size_t size, sos_ui32_t flags)
073 {
074   /* Look for a suitable pre-allocated kmalloc cache */
075   int i;
076   SOS_ASSERT_FATAL(size > 0);
077   for (i = 0 ; kmalloc_cache[i].object_size != 0 ; i ++)
078     {
079       if (kmalloc_cache[i].object_size >= size)
080         return sos_kmem_cache_alloc(kmalloc_cache[i].cache,
081                                     (flags
082                                      & SOS_KMALLOC_ATOMIC)?
083                                     SOS_KSLAB_ALLOC_ATOMIC:0);
084     }
085 
086   /* none found yet => we directly use the kmem_vmm subsystem to
087      allocate whole pages */
088   return sos_kmem_vmm_alloc(SOS_PAGE_ALIGN_SUP(size) / SOS_PAGE_SIZE,
089                             ( (flags
090                                & SOS_KMALLOC_ATOMIC)?
091                               SOS_KMEM_VMM_ATOMIC:0)
092                             | SOS_KMEM_VMM_MAP
093                             );
094 }
095 
096 
097 sos_ret_t sos_kfree(sos_vaddr_t vaddr)
098 {
099   /* The trouble here is that we aren't sure whether this object is a
100      slab object in a pre-allocated kmalloc cache, or an object
101      directly allocated as a kmem_vmm region. */
102   
103   /* We first pretend this object is allocated in a pre-allocated
104      kmalloc cache */
105   if (! sos_kmem_cache_free(vaddr))
106     return SOS_OK; /* Great ! We guessed right ! */
107     
108   /* Here we're wrong: it appears not to be an object in a
109      pre-allocated kmalloc cache. So we try to pretend this is a
110      kmem_vmm area */
111   return sos_kmem_vmm_free(vaddr);
112 }
113 
114 

source navigation ] diff markup ] identifier search ] general search ]