malloc_utils.g 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # This file is part of asmc, a bootstrapping OS with minimal seed
  2. # Copyright (C) 2018 Giovanni Mascellani <gio@debian.org>
  3. # https://gitlab.com/giomasce/asmc
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. fun realloc 2 {
  15. $newsize
  16. $ptr
  17. @newsize 1 param = ;
  18. @ptr 0 param = ;
  19. if ptr 0 == {
  20. newsize malloc ret ;
  21. }
  22. $size
  23. @size ptr _malloc_get_size = ;
  24. $newptr
  25. @newptr newsize malloc = ;
  26. $copysize
  27. @copysize size newsize min = ;
  28. copysize ptr newptr memcpy ;
  29. ptr free ;
  30. newptr ret ;
  31. }
  32. fun strdup 1 {
  33. $s
  34. @s 0 param = ;
  35. $len
  36. @len s strlen = ;
  37. $ptr
  38. @ptr len 1 + malloc = ;
  39. s ptr strcpy ;
  40. ptr ret ;
  41. }
  42. fun calloc 2 {
  43. $size
  44. $count
  45. @size 1 param = ;
  46. @count 0 param = ;
  47. $ptr
  48. @ptr size count * malloc = ;
  49. ptr 0 size count * memset ;
  50. ptr ret ;
  51. }