nfmalloc.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * nfmalloc.c - non-freeing malloc, used for in-core database
  4. *
  5. * Copyright (C) 1994,1995 Ian Jackson <iwj10@cus.cam.ac.uk>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public
  18. * License along with dpkg; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <config.h>
  24. #include <dpkg.h>
  25. #include <dpkg-db.h>
  26. #define ABLOCKSIZE 65536
  27. #define UNIQUE 4096
  28. union maxalign {
  29. long l; long double d;
  30. void *pv; char *pc; union maxalign *ps; void (*pf)(void);
  31. };
  32. static unsigned char *playground= 0;
  33. static long remaining= 0;
  34. static struct piece { struct piece *next; union maxalign space; } *pieces= 0;
  35. void nffreeall(void) {
  36. struct piece *a,*b;
  37. for (a=pieces; a; a=b) { b=a->next; free(a); }
  38. pieces= 0; remaining= 0;
  39. }
  40. static void *nfmalloc_sysmalloc(size_t size) {
  41. struct piece *alc;
  42. alc= m_malloc(size + sizeof(struct piece));
  43. alc->next= pieces; pieces= alc;
  44. return &alc->space;
  45. }
  46. #ifndef MDEBUG
  47. void *nfmalloc(size_t size) {
  48. #else
  49. static void *nfmalloc_r(size_t size) {
  50. #endif
  51. const size_t alignment= sizeof(union maxalign);
  52. size -= (size + alignment-1) % alignment;
  53. size += alignment-1;
  54. if (size > UNIQUE) return nfmalloc_sysmalloc(size);
  55. remaining -= size;
  56. if (remaining > 0) return playground -= size;
  57. playground= (unsigned char*)nfmalloc_sysmalloc(ABLOCKSIZE) + ABLOCKSIZE - size;
  58. remaining= ABLOCKSIZE-size;
  59. return playground;
  60. }
  61. #ifdef MDEBUG
  62. /* If we haven't switched off debugging we wrap nfmalloc in something
  63. * that fills the space with junk that may tell us what happened if
  64. * we dereference a wild pointer. It's better than leaving it full of
  65. * nulls, anyway.
  66. */
  67. void *nfmalloc(size_t size) {
  68. unsigned short *r, *r2, x;
  69. r= nfmalloc_r(size); r2=r; x= (unsigned short)size;
  70. while (size >= 2) { *r2++= x; size -= 2; }
  71. return r;
  72. }
  73. #endif
  74. char *nfstrsave(const char *string) {
  75. char *r;
  76. r= nfmalloc(strlen(string)+1);
  77. strcpy(r,string);
  78. return r;
  79. }