OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [fs/] [xfs/] [linux/] [kmem.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
 * Copyright (c) 2000-2002 Silicon Graphics, Inc.  All Rights Reserved.
3
 *
4
 * This program is free software; you can redistribute it and/or modify it
5
 * under the terms of version 2 of the GNU General Public License as
6
 * published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it would be useful, but
9
 * WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * Further, this software is distributed without any warranty that it is
13
 * free of the rightful claim of any third person regarding infringement
14
 * or the like.  Any license provided herein, whether implied or
15
 * otherwise, applies only to this software file.  Patent licenses, if
16
 * any, provided herein do not apply to combinations of this program with
17
 * other software, or any other product whatsoever.
18
 *
19
 * You should have received a copy of the GNU General Public License along
20
 * with this program; if not, write the Free Software Foundation, Inc., 59
21
 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22
 *
23
 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24
 * Mountain View, CA  94043, or:
25
 *
26
 * http://www.sgi.com
27
 *
28
 * For further information regarding this notice, see:
29
 *
30
 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31
 */
32
 
33
#include <linux/sched.h>
34
#include <linux/mm.h>
35
#include <linux/vmalloc.h>
36
#include <linux/slab.h>
37
 
38
#include "time.h"
39
#include "kmem.h"
40
 
41
#define DEF_PRIORITY    (6)
42
#define MAX_SLAB_SIZE   0x10000
43
#define MAX_SHAKE       8
44
 
45
static kmem_shake_func_t        shake_list[MAX_SHAKE];
46
static DECLARE_MUTEX(shake_sem);
47
 
48
kmem_shaker_t
49
kmem_shake_register(kmem_shake_func_t sfunc)
50
{
51
        int     i;
52
 
53
        down(&shake_sem);
54
        for (i = 0; i < MAX_SHAKE; i++) {
55
                if (shake_list[i] == NULL) {
56
                        shake_list[i] = sfunc;
57
                        break;
58
                }
59
        }
60
        if (i == MAX_SHAKE)
61
                BUG();
62
        up(&shake_sem);
63
 
64
        return (kmem_shaker_t)sfunc;
65
}
66
 
67
void
68
kmem_shake_deregister(kmem_shaker_t sfunc)
69
{
70
        int     i;
71
 
72
        down(&shake_sem);
73
        for (i = 0; i < MAX_SHAKE; i++) {
74
                if (shake_list[i] == (kmem_shake_func_t)sfunc)
75
                        break;
76
        }
77
        if (i == MAX_SHAKE)
78
                BUG();
79
        for (; i < MAX_SHAKE - 1; i++) {
80
                shake_list[i] = shake_list[i+1];
81
        }
82
        shake_list[i] = NULL;
83
        up(&shake_sem);
84
}
85
 
86
static __inline__ void kmem_shake(void)
87
{
88
        int     i;
89
 
90
        down(&shake_sem);
91
        for (i = 0; i < MAX_SHAKE && shake_list[i]; i++)
92
                (*shake_list[i])(0, 0);
93
        up(&shake_sem);
94
        delay(10);
95
}
96
 
97
void *
98
kmem_alloc(size_t size, int flags)
99
{
100
        int     shrink  = DEF_PRIORITY; /* # times to try to shrink cache */
101
        int     lflags  = kmem_flags_convert(flags);
102
        int     nosleep = flags & KM_NOSLEEP;
103
        void    *rval;
104
 
105
repeat:
106
        if (MAX_SLAB_SIZE < size) {
107
                /* Avoid doing filesystem sensitive stuff to get this */
108
                rval = __vmalloc(size, lflags, PAGE_KERNEL);
109
        } else {
110
                rval = kmalloc(size, lflags);
111
        }
112
 
113
        if (rval || nosleep)
114
                return rval;
115
 
116
        /*
117
         * KM_SLEEP callers don't expect a failure
118
         */
119
        if (shrink) {
120
                kmem_shake();
121
 
122
                shrink--;
123
                goto repeat;
124
        }
125
 
126
        rval = __vmalloc(size, lflags, PAGE_KERNEL);
127
        if (!rval && !nosleep)
128
                panic("kmem_alloc: NULL memory on KM_SLEEP request!");
129
 
130
        return rval;
131
}
132
 
133
void *
134
kmem_zalloc(size_t size, int flags)
135
{
136
        void    *ptr;
137
 
138
        ptr = kmem_alloc(size, flags);
139
 
140
        if (ptr)
141
                memset((char *)ptr, 0, (int)size);
142
 
143
        return (ptr);
144
}
145
 
146
void
147
kmem_free(void *ptr, size_t size)
148
{
149
        if (((unsigned long)ptr < VMALLOC_START) ||
150
            ((unsigned long)ptr >= VMALLOC_END)) {
151
                kfree(ptr);
152
        } else {
153
                vfree(ptr);
154
        }
155
}
156
 
157
void *
158
kmem_realloc(void *ptr, size_t newsize, size_t oldsize, int flags)
159
{
160
        void *new;
161
 
162
        new = kmem_alloc(newsize, flags);
163
        if (ptr) {
164
                if (new)
165
                        memcpy(new, ptr,
166
                                ((oldsize < newsize) ? oldsize : newsize));
167
                kmem_free(ptr, oldsize);
168
        }
169
 
170
        return new;
171
}
172
 
173
kmem_zone_t *
174
kmem_zone_init(int size, char *zone_name)
175
{
176
        return kmem_cache_create(zone_name, size, 0, 0, NULL, NULL);
177
}
178
 
179
void *
180
kmem_zone_alloc(kmem_zone_t *zone, int flags)
181
{
182
        int     shrink = DEF_PRIORITY;  /* # times to try to shrink cache */
183
        void    *ptr = NULL;
184
 
185
repeat:
186
        ptr = kmem_cache_alloc(zone, kmem_flags_convert(flags));
187
 
188
        if (ptr || (flags & KM_NOSLEEP))
189
                return ptr;
190
 
191
        /*
192
         * KM_SLEEP callers don't expect a failure
193
         */
194
        if (shrink) {
195
                kmem_shake();
196
 
197
                shrink--;
198
                goto repeat;
199
        }
200
 
201
        if (flags & KM_SLEEP)
202
                panic("kmem_zone_alloc: NULL memory on KM_SLEEP request!");
203
 
204
        return NULL;
205
}
206
 
207
void *
208
kmem_zone_zalloc(kmem_zone_t *zone, int flags)
209
{
210
        int     shrink = DEF_PRIORITY;  /* # times to try to shrink cache */
211
        void    *ptr = NULL;
212
 
213
repeat:
214
        ptr = kmem_cache_alloc(zone, kmem_flags_convert(flags));
215
 
216
        if (ptr) {
217
                memset(ptr, 0, kmem_cache_size(zone));
218
                return ptr;
219
        }
220
 
221
        if (flags & KM_NOSLEEP)
222
                return ptr;
223
 
224
        /*
225
         * KM_SLEEP callers don't expect a failure
226
         */
227
        if (shrink) {
228
                kmem_shake();
229
 
230
                shrink--;
231
                goto repeat;
232
        }
233
 
234
        if (flags & KM_SLEEP)
235
                panic("kmem_zone_zalloc: NULL memory on KM_SLEEP request!");
236
 
237
        return NULL;
238
}
239
 
240
void
241
kmem_zone_free(kmem_zone_t *zone, void *ptr)
242
{
243
        kmem_cache_free(zone, ptr);
244
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.