home *** CD-ROM | disk | FTP | other *** search
- /* sys/sbrk.c (emx+gcc) -- Copyright (c) 1992-1993 by Eberhard Mattes */
-
- #include <sys/emx.h>
- #include <os2emx.h>
- #include <errno.h>
- #include "syscalls.h"
-
- void *__sbrk (int incr)
- {
- ULONG rc;
- void *old;
- unsigned old_size, new_size;
- unsigned addr, rest, size;
-
- old = _sys_heap_brk;
- old_size = (char *)_sys_heap_brk - (char *)_sys_heap_base;
- new_size = old_size + incr;
- if (incr <= 0)
- {
- if (-incr > old_size)
- {
- errno = ENOMEM;
- return ((void *)(-1));
- }
- _sys_heap_brk = (char *)_sys_heap_brk + incr;
- return (old);
- }
- else if (new_size < old_size) /* overflow */
- {
- errno = ENOMEM;
- return ((void *)(-1));
- }
- else if (new_size > _sys_heap_size)
- {
- errno = ENOMEM;
- return ((void *)(-1));
- }
- else
- {
- addr = (unsigned)_sys_heap_brk;
- size = incr;
- if (addr & 0xfff)
- {
- rest = 0x1000 - (addr & 0xfff);
- if (incr <= rest)
- size = 0;
- else
- {
- size -= rest;
- addr += rest;
- }
- }
- if (size != 0)
- {
- rc = DosSetMem ((void *)addr, size, PAG_DEFAULT | PAG_COMMIT);
- if (rc != 0)
- {
- _sys_set_errno (rc);
- return ((void *)(-1));
- }
- }
- _sys_heap_brk = (char *)_sys_heap_brk + incr;
- return (old);
- }
- }
-