heap.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * @file heap.c
  3. * @brief System level setup help
  4. */
  5. /*******************************************************************************
  6. * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a
  9. * copy of this software and associated documentation files (the "Software"),
  10. * to deal in the Software without restriction, including without limitation
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12. * and/or sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included
  16. * in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  21. * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
  22. * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  23. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  24. * OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. * Except as contained in this notice, the name of Maxim Integrated
  27. * Products, Inc. shall not be used except as stated in the Maxim Integrated
  28. * Products, Inc. Branding Policy.
  29. *
  30. * The mere transfer of this software does not imply any licenses
  31. * of trade secrets, proprietary technology, copyrights, patents,
  32. * trademarks, maskwork rights, or any other form of intellectual
  33. * property whatsoever. Maxim Integrated Products, Inc. retains all
  34. * ownership rights.
  35. *
  36. * $Date: 2018-12-18 15:37:22 -0600 (Tue, 18 Dec 2018) $
  37. * $Revision: 40072 $
  38. *
  39. ******************************************************************************/
  40. /* **** Includes **** */
  41. #include <stdint.h>
  42. #include <errno.h>
  43. #include <unistd.h>
  44. /**
  45. * @brief sbrk
  46. * @detail Increase program data space
  47. * @detail Malloc and related functions depend on this
  48. */
  49. /* **** declarations **** */
  50. static char *heap_end = 0;
  51. extern unsigned int __HeapBase;
  52. extern unsigned int __HeapLimit;
  53. /* **** functions **** */
  54. caddr_t _sbrk(int incr)
  55. {
  56. char *prev_heap_end;
  57. if (heap_end == 0) {
  58. heap_end = (caddr_t)&__HeapBase;
  59. }
  60. prev_heap_end = heap_end;
  61. if ((unsigned int)(heap_end + incr) > (unsigned int)&__HeapLimit) {
  62. errno = ENOMEM;
  63. return (caddr_t) -1;
  64. }
  65. heap_end += incr;
  66. return (caddr_t) prev_heap_end;
  67. }