This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / fs / xfs / linux-2.6 / kmem.c
1 /*
2  * Copyright (c) 2000-2004 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/highmem.h>
37 #include <linux/swap.h>
38
39 #include "time.h"
40 #include "kmem.h"
41
42 #define MAX_VMALLOCS    6
43 #define MAX_SLAB_SIZE   0x20000
44
45
46 void *
47 kmem_alloc(size_t size, int flags)
48 {
49         int     retries = 0, lflags = kmem_flags_convert(flags);
50         void    *ptr;
51
52         do {
53                 if (size < MAX_SLAB_SIZE || retries > MAX_VMALLOCS)
54                         ptr = kmalloc(size, lflags);
55                 else
56                         ptr = __vmalloc(size, lflags, PAGE_KERNEL);
57                 if (ptr || (flags & (KM_MAYFAIL|KM_NOSLEEP)))
58                         return ptr;
59                 if (!(++retries % 100))
60                         printk(KERN_ERR "possible deadlock in %s (mode:0x%x)\n",
61                                         __FUNCTION__, lflags);
62         } while (1);
63 }
64
65 void *
66 kmem_zalloc(size_t size, int flags)
67 {
68         void    *ptr;
69
70         ptr = kmem_alloc(size, flags);
71         if (ptr)
72                 memset((char *)ptr, 0, (int)size);
73         return ptr;
74 }
75
76 void
77 kmem_free(void *ptr, size_t size)
78 {
79         if (((unsigned long)ptr < VMALLOC_START) ||
80             ((unsigned long)ptr >= VMALLOC_END)) {
81                 kfree(ptr);
82         } else {
83                 vfree(ptr);
84         }
85 }
86
87 void *
88 kmem_realloc(void *ptr, size_t newsize, size_t oldsize, int flags)
89 {
90         void    *new;
91
92         new = kmem_alloc(newsize, flags);
93         if (ptr) {
94                 if (new)
95                         memcpy(new, ptr,
96                                 ((oldsize < newsize) ? oldsize : newsize));
97                 kmem_free(ptr, oldsize);
98         }
99         return new;
100 }
101
102 void *
103 kmem_zone_alloc(kmem_zone_t *zone, int flags)
104 {
105         int     retries = 0, lflags = kmem_flags_convert(flags);
106         void    *ptr;
107
108         do {
109                 ptr = kmem_cache_alloc(zone, lflags);
110                 if (ptr || (flags & (KM_MAYFAIL|KM_NOSLEEP)))
111                         return ptr;
112                 if (!(++retries % 100))
113                         printk(KERN_ERR "possible deadlock in %s (mode:0x%x)\n",
114                                         __FUNCTION__, lflags);
115         } while (1);
116 }
117
118 void *
119 kmem_zone_zalloc(kmem_zone_t *zone, int flags)
120 {
121         void    *ptr;
122
123         ptr = kmem_zone_alloc(zone, flags);
124         if (ptr)
125                 memset((char *)ptr, 0, kmem_cache_size(zone));
126         return ptr;
127 }