Magellan Linux

Contents of /trunk/mkinitrd-magellan/klibc/usr/klibc/sbrk.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 532 - (show annotations) (download)
Sat Sep 1 22:45:15 2007 UTC (16 years, 9 months ago) by niro
File MIME type: text/plain
File size: 930 byte(s)
-import if magellan mkinitrd; it is a fork of redhats mkinitrd-5.0.8 with all magellan patches and features; deprecates magellan-src/mkinitrd

1 /* sbrk.c - Change data segment size */
2
3 /* Written 2000 by Werner Almesberger */
4 /* Modified 2003-2004 for klibc by H. Peter Anvin */
5
6 #include <stddef.h>
7 #include <unistd.h>
8 #include <inttypes.h>
9 #include <errno.h>
10 #include "malloc.h"
11
12 #if !_KLIBC_NO_MMU /* uClinux doesn't have brk() */
13
14 char *__current_brk; /* Common with brk.c */
15
16 /* p is an address, a is alignment; must be a power of 2 */
17 static inline void *align_up(void *p, uintptr_t a)
18 {
19 return (void *)(((uintptr_t) p + a - 1) & ~(a - 1));
20 }
21
22 void *sbrk(ptrdiff_t increment)
23 {
24 char *start, *end, *new_brk;
25
26 if (!__current_brk)
27 __current_brk = __brk(NULL);
28
29 start = align_up(__current_brk, _KLIBC_SBRK_ALIGNMENT);
30 end = start + increment;
31
32 new_brk = __brk(end);
33
34 if (new_brk == (void *)-1)
35 return (void *)-1;
36 else if (new_brk < end) {
37 errno = ENOMEM;
38 return (void *)-1;
39 }
40
41 __current_brk = new_brk;
42 return start;
43 }
44
45 #endif /* !_KLIBC_NO_MMU */