Advertisement
doragasu

Syscalls

Feb 13th, 2013
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.33 KB | None | 0 0
  1. /// Error holder
  2. extern int errno;
  3. /// End of heap
  4. char *heap_end = 0;
  5.  
  6.  
  7. caddr_t _sbrk(unsigned int incr)
  8. {
  9.     /// Start of heap
  10.     extern char _heap_bottom;
  11.     /// Start of stack (also end of heap)
  12.     extern char _stack_bottom;
  13.     /// Temporal heap pointer
  14.     char *prev_heap_end;
  15.     tBoolean intStat;
  16.  
  17.     /// Disable interrupts to avoid heap corruption if _sbrk() is called from
  18.     /// inside an interrupt.
  19.     intStat = MAP_IntMasterDisable();
  20.  
  21.     /// Heap initialization
  22.     if (heap_end == 0) heap_end = &_heap_bottom;
  23.  
  24.     /// Store current heap pointer
  25.     prev_heap_end = heap_end;
  26.  
  27.     if ((heap_end + incr) > &_stack_bottom)
  28.     {
  29.         /// Error: Not enough heap space available
  30.         errno = ENOMEM;
  31.         /// Enable interrupts if they were already enabled when entering
  32.         if (intStat == false) MAP_IntMasterEnable();
  33.         /// \todo Should we return -1?
  34.         return (caddr_t)0;
  35.     }
  36.  
  37.     /// Allocate requested heap
  38.     heap_end += incr;
  39.  
  40.     /// Enable interrupts if they were already enabled when entering
  41.     if (intStat == false) MAP_IntMasterEnable();
  42.     /// Return pointer to allocated data
  43.     return (caddr_t)prev_heap_end;
  44. }
  45.  
  46. int _write(int file, char *ptr, unsigned int len)
  47. {
  48.     unsigned int i;
  49.     for(i = 0; i < len; i++) MAP_UARTCharPut(UART0_BASE, ptr[i]);
  50.     return i;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement