stdlib.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. ** Copyright (c) 2001-2007 Expat maintainers.
  3. **
  4. ** Permission is hereby granted, free of charge, to any person obtaining
  5. ** a copy of this software and associated documentation files (the
  6. ** "Software"), to deal in the Software without restriction, including
  7. ** without limitation the rights to use, copy, modify, merge, publish,
  8. ** distribute, sublicense, and/or sell copies of the Software, and to
  9. ** permit persons to whom the Software is furnished to do so, subject to
  10. ** the following conditions:
  11. **
  12. ** The above copyright notice and this permission notice shall be included
  13. ** in all copies or substantial portions of the Software.
  14. **
  15. ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <stdlib.h>
  24. #include <exec/memory.h>
  25. #include <proto/exec.h>
  26. #include <proto/utility.h>
  27. void * malloc (size_t len)
  28. {
  29. uint32 size = sizeof(uint32) + len;
  30. uint32 *mem = AllocMem(size, MEMF_SHARED);
  31. if ( mem != 0 ) {
  32. *mem = size;
  33. ++mem;
  34. }
  35. return mem;
  36. }
  37. void * realloc (void * mem, size_t len2)
  38. {
  39. if ( mem == 0 ) {
  40. return malloc(len2);
  41. }
  42. if ( len2 == 0 ) {
  43. free(mem);
  44. return 0;
  45. }
  46. void * new_mem = malloc(len2);
  47. if ( new_mem == 0 ) {
  48. return 0;
  49. }
  50. uint32 mem_size = *(((uint32*)mem) - 1);
  51. CopyMem(mem, new_mem, mem_size);
  52. free(mem);
  53. return new_mem;
  54. }
  55. void free (void * mem)
  56. {
  57. if ( mem != 0 ) {
  58. uint32 * size_ptr = ((uint32*)mem) - 1;
  59. FreeMem(size_ptr, *size_ptr);
  60. }
  61. }
  62. int memcmp (const void * a, const void * b, size_t len)
  63. {
  64. size_t i;
  65. int diff;
  66. for ( i = 0; i < len; ++i ) {
  67. diff = *((uint8 *)a++) - *((uint8 *)b++);
  68. if ( diff ) {
  69. return diff;
  70. }
  71. }
  72. return 0;
  73. }
  74. void * memcpy (void * t, const void * a, size_t len)
  75. {
  76. CopyMem((APTR)a, t, len);
  77. return t;
  78. }
  79. void * memmove (void * t1, const void * t2, size_t len)
  80. {
  81. MoveMem((APTR)t2, t1, len);
  82. return t1;
  83. }
  84. void * memset (void * t, int c, size_t len)
  85. {
  86. return SetMem(t, c, len);
  87. }