]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | #include <linux/blkdev.h> | |
39 | ||
40 | #include "time.h" | |
41 | #include "kmem.h" | |
42 | ||
43 | #define MAX_VMALLOCS 6 | |
44 | #define MAX_SLAB_SIZE 0x20000 | |
45 | ||
46 | ||
47 | void * | |
dd0fc66f | 48 | kmem_alloc(size_t size, gfp_t flags) |
1da177e4 | 49 | { |
760dea67 CH |
50 | int retries = 0; |
51 | unsigned int lflags = kmem_flags_convert(flags); | |
52 | void *ptr; | |
1da177e4 LT |
53 | |
54 | do { | |
55 | if (size < MAX_SLAB_SIZE || retries > MAX_VMALLOCS) | |
56 | ptr = kmalloc(size, lflags); | |
57 | else | |
58 | ptr = __vmalloc(size, lflags, PAGE_KERNEL); | |
59 | if (ptr || (flags & (KM_MAYFAIL|KM_NOSLEEP))) | |
60 | return ptr; | |
61 | if (!(++retries % 100)) | |
62 | printk(KERN_ERR "XFS: possible memory allocation " | |
63 | "deadlock in %s (mode:0x%x)\n", | |
64 | __FUNCTION__, lflags); | |
65 | blk_congestion_wait(WRITE, HZ/50); | |
66 | } while (1); | |
67 | } | |
68 | ||
69 | void * | |
dd0fc66f | 70 | kmem_zalloc(size_t size, gfp_t flags) |
1da177e4 LT |
71 | { |
72 | void *ptr; | |
73 | ||
74 | ptr = kmem_alloc(size, flags); | |
75 | if (ptr) | |
76 | memset((char *)ptr, 0, (int)size); | |
77 | return ptr; | |
78 | } | |
79 | ||
80 | void | |
81 | kmem_free(void *ptr, size_t size) | |
82 | { | |
83 | if (((unsigned long)ptr < VMALLOC_START) || | |
84 | ((unsigned long)ptr >= VMALLOC_END)) { | |
85 | kfree(ptr); | |
86 | } else { | |
87 | vfree(ptr); | |
88 | } | |
89 | } | |
90 | ||
91 | void * | |
760dea67 | 92 | kmem_realloc(void *ptr, size_t newsize, size_t oldsize, |
dd0fc66f | 93 | gfp_t flags) |
1da177e4 LT |
94 | { |
95 | void *new; | |
96 | ||
97 | new = kmem_alloc(newsize, flags); | |
98 | if (ptr) { | |
99 | if (new) | |
100 | memcpy(new, ptr, | |
101 | ((oldsize < newsize) ? oldsize : newsize)); | |
102 | kmem_free(ptr, oldsize); | |
103 | } | |
104 | return new; | |
105 | } | |
106 | ||
107 | void * | |
dd0fc66f | 108 | kmem_zone_alloc(kmem_zone_t *zone, gfp_t flags) |
1da177e4 | 109 | { |
760dea67 CH |
110 | int retries = 0; |
111 | unsigned int lflags = kmem_flags_convert(flags); | |
112 | void *ptr; | |
1da177e4 LT |
113 | |
114 | do { | |
115 | ptr = kmem_cache_alloc(zone, lflags); | |
116 | if (ptr || (flags & (KM_MAYFAIL|KM_NOSLEEP))) | |
117 | return ptr; | |
118 | if (!(++retries % 100)) | |
119 | printk(KERN_ERR "XFS: possible memory allocation " | |
120 | "deadlock in %s (mode:0x%x)\n", | |
121 | __FUNCTION__, lflags); | |
122 | blk_congestion_wait(WRITE, HZ/50); | |
123 | } while (1); | |
124 | } | |
125 | ||
126 | void * | |
dd0fc66f | 127 | kmem_zone_zalloc(kmem_zone_t *zone, gfp_t flags) |
1da177e4 LT |
128 | { |
129 | void *ptr; | |
130 | ||
131 | ptr = kmem_zone_alloc(zone, flags); | |
132 | if (ptr) | |
133 | memset((char *)ptr, 0, kmem_cache_size(zone)); | |
134 | return ptr; | |
135 | } |