Subversion Repositories HelenOS

Rev

Rev 2123 | Rev 2745 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2123 Rev 2124
Line 109... Line 109...
109
#include <print.h>
109
#include <print.h>
110
#include <arch.h>
110
#include <arch.h>
111
#include <panic.h>
111
#include <panic.h>
112
#include <debug.h>
112
#include <debug.h>
113
#include <bitops.h>
113
#include <bitops.h>
-
 
114
#include <macros.h>
114
 
115
 
115
SPINLOCK_INITIALIZE(slab_cache_lock);
116
SPINLOCK_INITIALIZE(slab_cache_lock);
116
static LIST_INITIALIZE(slab_cache_list);
117
static LIST_INITIALIZE(slab_cache_list);
117
 
118
 
118
/** Magazine cache */
119
/** Magazine cache */
Line 125... Line 126...
125
 *   as all slab structures are 'small' - control structures of
126
 *   as all slab structures are 'small' - control structures of
126
 *   their caches do not require further allocation
127
 *   their caches do not require further allocation
127
 */
128
 */
128
static slab_cache_t *slab_extern_cache;
129
static slab_cache_t *slab_extern_cache;
129
/** Caches for malloc */
130
/** Caches for malloc */
130
static slab_cache_t *malloc_caches[SLAB_MAX_MALLOC_W-SLAB_MIN_MALLOC_W+1];
131
static slab_cache_t *malloc_caches[SLAB_MAX_MALLOC_W - SLAB_MIN_MALLOC_W + 1];
131
char *malloc_names[] =  {
132
char *malloc_names[] =  {
-
 
133
    "malloc-16",
-
 
134
    "malloc-32",
-
 
135
    "malloc-64",
132
    "malloc-16","malloc-32","malloc-64","malloc-128",
136
    "malloc-128",
133
    "malloc-256","malloc-512","malloc-1K","malloc-2K",
137
    "malloc-256",
-
 
138
    "malloc-512",
-
 
139
    "malloc-1K",
-
 
140
    "malloc-2K",
-
 
141
    "malloc-4K",
-
 
142
    "malloc-8K",
-
 
143
    "malloc-16K",
134
    "malloc-4K","malloc-8K","malloc-16K","malloc-32K",
144
    "malloc-32K",
-
 
145
    "malloc-64K",
135
    "malloc-64K","malloc-128K","malloc-256K"
146
    "malloc-128K",
-
 
147
    "malloc-256K"
136
};
148
};
137
 
149
 
138
/** Slab descriptor */
150
/** Slab descriptor */
139
typedef struct {
151
typedef struct {
140
    slab_cache_t *cache;    /**< Pointer to parent cache. */
152
    slab_cache_t *cache;    /**< Pointer to parent cache. */
141
    link_t link;        /**< List of full/partial slabs. */
153
    link_t link;        /**< List of full/partial slabs. */
142
    void *start;        /**< Start address of first available item. */
154
    void *start;        /**< Start address of first available item. */
143
    count_t available;  /**< Count of available items in this slab. */
155
    count_t available;  /**< Count of available items in this slab. */
144
    index_t nextavail;  /**< The index of next available item. */
156
    index_t nextavail;  /**< The index of next available item. */
145
}slab_t;
157
} slab_t;
146
 
158
 
147
#ifdef CONFIG_DEBUG
159
#ifdef CONFIG_DEBUG
148
static int _slab_initialized = 0;
160
static int _slab_initialized = 0;
149
#endif
161
#endif
150
 
162
 
Line 211... Line 223...
211
}
223
}
212
 
224
 
213
/** Map object to slab structure */
225
/** Map object to slab structure */
214
static slab_t * obj2slab(void *obj)
226
static slab_t * obj2slab(void *obj)
215
{
227
{
216
    return (slab_t *)frame_get_parent(ADDR2PFN(KA2PA(obj)), 0);
228
    return (slab_t *) frame_get_parent(ADDR2PFN(KA2PA(obj)), 0);
217
}
229
}
218
 
230
 
219
/**************************************/
231
/**************************************/
220
/* Slab functions */
232
/* Slab functions */
221
 
233
 
Line 758... Line 770...
758
}
770
}
759
 
771
 
760
/** Return slab object to cache */
772
/** Return slab object to cache */
761
void slab_free(slab_cache_t *cache, void *obj)
773
void slab_free(slab_cache_t *cache, void *obj)
762
{
774
{
763
    _slab_free(cache,obj,NULL);
775
    _slab_free(cache, obj, NULL);
764
}
776
}
765
 
777
 
766
/* Go through all caches and reclaim what is possible */
778
/* Go through all caches and reclaim what is possible */
767
count_t slab_reclaim(int flags)
779
count_t slab_reclaim(int flags)
768
{
780
{
Line 876... Line 888...
876
 
888
 
877
/**************************************/
889
/**************************************/
878
/* kalloc/kfree functions             */
890
/* kalloc/kfree functions             */
879
void * malloc(unsigned int size, int flags)
891
void * malloc(unsigned int size, int flags)
880
{
892
{
881
    int idx;
-
 
882
 
-
 
883
    ASSERT(_slab_initialized);
893
    ASSERT(_slab_initialized);
884
    ASSERT(size && size <= (1 << SLAB_MAX_MALLOC_W));
894
    ASSERT(size && size <= (1 << SLAB_MAX_MALLOC_W));
885
   
895
   
886
    if (size < (1 << SLAB_MIN_MALLOC_W))
896
    if (size < (1 << SLAB_MIN_MALLOC_W))
887
        size = (1 << SLAB_MIN_MALLOC_W);
897
        size = (1 << SLAB_MIN_MALLOC_W);
888
 
898
 
889
    idx = fnzb(size-1) - SLAB_MIN_MALLOC_W + 1;
899
    int idx = fnzb(size - 1) - SLAB_MIN_MALLOC_W + 1;
890
 
900
 
891
    return slab_alloc(malloc_caches[idx], flags);
901
    return slab_alloc(malloc_caches[idx], flags);
892
}
902
}
893
 
903
 
894
void free(void *obj)
904
void * realloc(void *ptr, unsigned int size, int flags)
895
{
905
{
-
 
906
    ASSERT(_slab_initialized);
-
 
907
    ASSERT(size <= (1 << SLAB_MAX_MALLOC_W));
-
 
908
   
-
 
909
    void *new_ptr;
-
 
910
   
-
 
911
    if (size > 0) {
-
 
912
        if (size < (1 << SLAB_MIN_MALLOC_W))
-
 
913
            size = (1 << SLAB_MIN_MALLOC_W);
-
 
914
        int idx = fnzb(size - 1) - SLAB_MIN_MALLOC_W + 1;
-
 
915
       
-
 
916
        new_ptr = slab_alloc(malloc_caches[idx], flags);
-
 
917
    } else
896
    slab_t *slab;
918
        new_ptr = NULL;
-
 
919
   
-
 
920
    if ((new_ptr != NULL) && (ptr != NULL)) {
-
 
921
        slab_t *slab = obj2slab(ptr);
-
 
922
        memcpy(new_ptr, ptr, min(size, slab->cache->size));
-
 
923
    }
-
 
924
   
-
 
925
    if (ptr != NULL)
-
 
926
        free(ptr);
-
 
927
   
-
 
928
    return new_ptr;
-
 
929
}
897
 
930
 
-
 
931
void free(void *ptr)
-
 
932
{
898
    if (!obj)
933
    if (!ptr)
899
        return;
934
        return;
900
 
935
 
901
    slab = obj2slab(obj);
936
    slab_t *slab = obj2slab(ptr);
902
    _slab_free(slab->cache, obj, slab);
937
    _slab_free(slab->cache, ptr, slab);
903
}
938
}
904
 
939
 
905
/** @}
940
/** @}
906
 */
941
 */