mallocs.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* classes: src_files
  2. * Copyright (C) 1995,1997,1998,2000,2001, 2006 Free Software Foundation, Inc.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include "libguile/_scm.h"
  22. #include "libguile/ports.h"
  23. #include "libguile/smob.h"
  24. #include "libguile/mallocs.h"
  25. #ifdef HAVE_MALLOC_H
  26. #include <malloc.h>
  27. #endif
  28. #ifdef HAVE_UNISTD_H
  29. #include <unistd.h>
  30. #endif
  31. scm_t_bits scm_tc16_malloc;
  32. static size_t
  33. malloc_free (SCM ptr)
  34. {
  35. if (SCM_MALLOCDATA (ptr))
  36. free (SCM_MALLOCDATA (ptr));
  37. return 0;
  38. }
  39. static int
  40. malloc_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
  41. {
  42. scm_puts("#<malloc ", port);
  43. scm_uintprint (SCM_SMOB_DATA (exp), 16, port);
  44. scm_putc('>', port);
  45. return 1;
  46. }
  47. SCM
  48. scm_malloc_obj (size_t n)
  49. {
  50. scm_t_bits mem = n ? (scm_t_bits) scm_gc_malloc (n, "malloc smob") : 0;
  51. if (n && !mem)
  52. return SCM_BOOL_F;
  53. SCM_RETURN_NEWSMOB (scm_tc16_malloc, mem);
  54. }
  55. void
  56. scm_init_mallocs ()
  57. {
  58. scm_tc16_malloc = scm_make_smob_type ("malloc", 0);
  59. scm_set_smob_free (scm_tc16_malloc, malloc_free);
  60. scm_set_smob_print (scm_tc16_malloc, malloc_print);
  61. }
  62. /*
  63. Local Variables:
  64. c-file-style: "gnu"
  65. End:
  66. */