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 ]

Diff markup

Differences between /sos/kmalloc.c (Article 9.5) and /sos/kmalloc.c (Article 7)


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

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