mallocs.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* classes: src_files
  2. * Copyright (C) 1995, 1997, 1998, 2000, 2002 Free Software Foundation, Inc.
  3. *
  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 2, or (at your option)
  7. * any later version.
  8. *
  9. * This program 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
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this software; see the file COPYING. If not, write to
  16. * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  17. * Boston, MA 02111-1307 USA */
  18. #include <stdio.h>
  19. #include "libguile/_scm.h"
  20. #include "libguile/ports.h"
  21. #include "libguile/smob.h"
  22. #include "libguile/mallocs.h"
  23. #ifdef HAVE_MALLOC_H
  24. #include <malloc.h>
  25. #endif
  26. #ifdef HAVE_UNISTD_H
  27. #include <unistd.h>
  28. #endif
  29. static scm_sizet
  30. fmalloc(SCM ptr)
  31. {
  32. if (SCM_MALLOCDATA (ptr))
  33. free (SCM_MALLOCDATA (ptr));
  34. return 0;
  35. }
  36. static int
  37. prinmalloc (SCM exp,SCM port,scm_print_state *pstate)
  38. {
  39. scm_puts("#<malloc ", port);
  40. scm_intprint (SCM_CELL_WORD_1 (exp), 16, port);
  41. scm_putc('>', port);
  42. return 1;
  43. }
  44. int scm_tc16_malloc;
  45. SCM
  46. scm_malloc_obj (scm_sizet n)
  47. {
  48. scm_bits_t mem = n ? (scm_bits_t) malloc (n) : 0;
  49. if (n && !mem)
  50. {
  51. SCM_ALLOW_INTS;
  52. return SCM_BOOL_F;
  53. }
  54. SCM_RETURN_NEWSMOB (scm_tc16_malloc, mem);
  55. }
  56. void
  57. scm_init_mallocs ()
  58. {
  59. scm_tc16_malloc = scm_make_smob_type_mfpe ("malloc", 0,
  60. NULL, fmalloc, prinmalloc, NULL);
  61. }
  62. /*
  63. Local Variables:
  64. c-file-style: "gnu"
  65. End:
  66. */