File: s_kmem.c

package info (click to toggle)
oskit 0.97.20000202-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 58,008 kB
  • ctags: 172,612
  • sloc: ansic: 832,827; asm: 7,640; sh: 3,920; yacc: 3,664; perl: 1,457; lex: 427; makefile: 337; csh: 141; awk: 78
file content (197 lines) | stat: -rw-r--r-- 4,579 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * Copyright (c) 1996-1999 The University of Utah and the Flux Group.
 * 
 * This file is part of the OSKit Linux Glue Libraries, which are free
 * software, also known as "open source;" you can redistribute them and/or
 * modify them under the terms of the GNU General Public License (GPL),
 * version 2, as published by the Free Software Foundation (FSF).
 * 
 * The OSKit is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GPL for more details.  You should have
 * received a copy of the GPL along with the OSKit; see the file COPYING.  If
 * not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
 */
/*
 * Linux kernel memory allocation.
 */

#ifndef OSKIT
#define OSKIT
#endif

#include <linux/sched.h>
#include <linux/malloc.h>
#include <linux/delay.h>

#include "shared.h"
#include "osenv.h"

/* XXX are all these flags really necessary? */
#define KMALLOC_FLAGS	(OSENV_AUTO_SIZE | OSENV_PHYS_WIRED |	\
			 OSENV_VIRT_EQ_PHYS | OSENV_PHYS_CONTIG)

/*
 * Invoke the OS's memory allocation services,
 * saving the current pointer around the possibly-blocking call.
 */
void *
oskit_linux_mem_alloc(oskit_size_t size, osenv_memflags_t flags, unsigned align)
{
	struct task_struct *cur = current;
	void *mem;
	mem = osenv_mem_alloc(size, flags, align);
	current = cur;
	return mem;
}

void
oskit_linux_mem_free(void *block, osenv_memflags_t flags, oskit_size_t size)
{
	struct task_struct *cur = current;
	osenv_mem_free(block, flags, size);
	current = cur;
}

/*
 * Allocate wired down kernel memory.
 */
void *
kmalloc(unsigned int size, int priority)
{
	int flags;
	void *p;

	flags = KMALLOC_FLAGS;
	if (priority & GFP_ATOMIC)
		flags |= OSENV_NONBLOCKING;
	if (priority & GFP_DMA)
		flags |= OSENV_ISADMA_MEM;
	p = oskit_linux_mem_alloc(size, flags, 0);
	return (p);
}

/*
 * Free kernel memory.
 * XXX this should probably pass the same flags as kmalloc.  It doesn't matter
 *     right now since osenv_mem_free only listens to OSENV_AUTO_FLAGS.
 */
void
kfree(const void *p)
{
	oskit_linux_mem_free((void *) p, KMALLOC_FLAGS, 0);
}

/*
 * Allocate physically contiguous pages.
 */
unsigned long
__get_free_pages(int priority, unsigned long order)
{
	int flags;
	void *p;

	flags = OSENV_PHYS_WIRED|OSENV_VIRT_EQ_PHYS|OSENV_PHYS_CONTIG;
	if (priority & GFP_ATOMIC)
		flags |= OSENV_NONBLOCKING;
	if (priority & GFP_DMA)
		flags |= OSENV_ISADMA_MEM;
	p = oskit_linux_mem_alloc(PAGE_SIZE << order, flags, PAGE_SIZE);
	return ((unsigned long)p);
}

/*
 * Free pages.
 */
void
free_pages(unsigned long addr, unsigned long order)
{
	oskit_linux_mem_free((void *)addr,
			     OSENV_PHYS_WIRED|OSENV_VIRT_EQ_PHYS|OSENV_PHYS_CONTIG,
			     PAGE_SIZE << order);
}

/*
 * Allocate virtually contiguous memory.
 * The underlying physical pages do not need to be contiguous.
 */
void *
vmalloc(unsigned long size)
{
	return oskit_linux_mem_alloc(size,
				     OSENV_AUTO_SIZE|OSENV_PHYS_WIRED,
				     0);
}

void
vfree(void *addr)
{
	oskit_linux_mem_free(addr, OSENV_AUTO_SIZE|OSENV_PHYS_WIRED, 0);
}

/*
 * Slab stuff. No caching, just fake it best we can.
 */
struct slab_stuff {
	char	*name;
	int	size;
};

kmem_cache_t *
kmem_cache_create(const char *name, size_t size, size_t offset,
		  unsigned long flags,
		  void (*ctor)(void*, kmem_cache_t *, unsigned long),
		  void (*dtor)(void*, kmem_cache_t *, unsigned long))
{
	struct slab_stuff	*stuffp;

	if (ctor || dtor || offset)
		osenv_panic("kmem_cache_create: Cannot handle ctor or dtor");

	stuffp = kmalloc(sizeof(struct slab_stuff) + strlen(name) + 1, 0);
	if (!stuffp)
		osenv_panic("kmem_cache_create: No memory left");

	stuffp->name = (char *) (stuffp + 1);
	stuffp->size = size;
	memcpy(stuffp->name, name, strlen(name));

	/* printk("kmem_cache_create: %s %d %p\n", name, size, stuffp); */

	return (kmem_cache_t *) stuffp;
}

void *
kmem_cache_alloc(kmem_cache_t *cachep, int flags)
{
	struct slab_stuff	*stuffp = (struct slab_stuff *) cachep;
	void			*obj;

	obj = kmalloc(stuffp->size, flags);

	/* printk("kmem_cache_alloc: %s %d %p\n",
	          stuffp->name, stuffp->size, obj); */

	return obj;
}

void
kmem_cache_free(kmem_cache_t *cachep, void *objp)
{
#if 0
	struct slab_stuff	*stuffp = (struct slab_stuff *) cachep;

	/* printk("kmem_cache_free: %s %d %p\n",
	          stuffp->name, stuffp->size, objp); */
#endif

	kfree(objp);
}

void
kfree_s(const void *objp, size_t size)
{
	/* printk("kfree_s: %d %p\n", size, objp); */

	kfree(objp);
}