brk.c
上传用户:super_houu
上传日期:2008-09-21
资源大小:4099k
文件大小:3k
- /*
- * Paradigm C/C++ Run-Time Library - Version 5.0
- *
- * Copyright (c) 1998 Paradigm Systems. All rights reserved.
- * Portions Copyright (c) 1996 Borland International.
- *
- * $Revision: 2 $
- * $Workfile: brk.c $
- *
- * function(s)
- * brk - memory model dependent hook to _brk or __brk
- * sbrk - memory model dependent hook to _sbrk or __sbrk
- * __brk - changes data-segment space allocation on the near heap
- * __sbrk - changes data-segment space allocation on the near heap
- */
- #include "Config.h" // Global Configuration - do not remove!
- #pragma inline
- #include "ServicesIncludeasmrules.h"
- #include <alloc.h>
- #include <errno.h>
- #include "ServicesInclude_heap.h"
- #pragma codeseg _TEXT "CODE"
- #pragma alias ( ___brk, __RtlBrk )
- #pragma alias ( ___sbrk, __RtlSbrk )
- /*--------------------------------------------------------------------------*
- Name __brk - changes data-segment space allocation on the near heap
- Usage int __brk(void *endds);
- Prototype in alloc.h
- Description __brk sets the break value to endds and changes the
- allocated space accordingly
- Return value success : 0
- failure : -1 and errno set to ENOMEM (Not enough core)
- *---------------------------------------------------------------------------*/
- int __near _RtlBrk( void * __near addr )
- {
- unsigned int heaplimit,brklvl;
- heaplimit = ((struct POOL __near *)_heapbase)->heaplimit;
-
- asm mov ax, addr
- asm mov dx, heaplimit
- asm cmp ax, dx
- asm jnb brkerr
- asm mov brklvl, ax
- ((struct POOL __near*)_heapbase)->brklvl = brklvl;
- return 0 ;
- brkerr:
- errno = ENOMEM ;
- return -1 ;
- }
- /*--------------------------------------------------------------------------*
- Name __sbrk - changes data-segment space allocation on the near heap
- Usage void *__sbrk(long incr);
- Prototype in alloc.h
- Description sbrk adds incr bytes to the break value and changes the
- allocated space accordingly. incr can be negative, in
- which case the amount of allocated space is decreased.
- Return value success : the old break value
- failure : -1 and errno set to ENOMEM (Not enough core)
- *---------------------------------------------------------------------------*/
- void * __near _RtlSbrk( long incr )
- {
- unsigned int brklvl = ((struct POOL __near*)_heapbase)->brklvl;
- unsigned int heaplimit = ((struct POOL __near*)_heapbase)->heaplimit;
-
- asm mov ax, W0(incr)
- asm mov dx, W1(incr)
- asm add ax, brklvl
- asm adc dx, 0
- asm mov cx, ax
- asm or dx, dx
- asm jnz sbrkErr
- asm cmp cx, heaplimit
- asm jnb sbrkErr
- asm xchg brklvl, ax
- asm push ax
- ((struct POOL __near*)_heapbase)->brklvl = brklvl;
- asm pop ax
- return (void *) _AX ;
-
- sbrkErr:
- errno = ENOMEM ;
- return ( (void *) -1 ) ;
- }
- /*--------------------------------------------------------------------------*
- In the small data models brk and sbrk are hooks to __brk and
- __sbrk respectively. __brk and __sbrk work with the near heap
- by altering the value of __brklvl, the break level.
- *---------------------------------------------------------------------------*/
- int brk( void* addr )
- {
- return __brk( addr ) ;
- }
- void* sbrk( int incr )
- {
- return __sbrk( (long) incr ) ;
- }