/usr.bin/xlint/lint2/mem2.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 97 lines · 42 code · 14 blank · 41 comment · 4 complexity · 0ae361d2df3f811d859fc71ce2161cfd MD5 · raw file

  1. /* $NetBSD: mem2.c,v 1.6 2002/01/21 19:49:52 tv Exp $ */
  2. /*
  3. * Copyright (c) 1994, 1995 Jochen Pohl
  4. * All Rights Reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. All advertising materials mentioning features or use of this software
  15. * must display the following acknowledgement:
  16. * This product includes software developed by Jochen Pohl for
  17. * The NetBSD Project.
  18. * 4. The name of the author may not be used to endorse or promote products
  19. * derived from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  22. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  23. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  30. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include <sys/cdefs.h>
  33. #if defined(__RCSID) && !defined(lint)
  34. __RCSID("$NetBSD: mem2.c,v 1.6 2002/01/21 19:49:52 tv Exp $");
  35. #endif
  36. __FBSDID("$FreeBSD$");
  37. #include <sys/param.h>
  38. #include <sys/types.h>
  39. #include <sys/mman.h>
  40. #include <err.h>
  41. #include <unistd.h>
  42. #include <string.h>
  43. #include "lint2.h"
  44. /* length of new allocated memory blocks */
  45. static size_t mblklen;
  46. /* offset of next free byte in mbuf */
  47. static size_t nxtfree;
  48. /* current buffer to server memory requests from */
  49. static void *mbuf;
  50. void
  51. initmem(void)
  52. {
  53. int pgsz;
  54. pgsz = getpagesize();
  55. mblklen = ((MBLKSIZ + pgsz - 1) / pgsz) * pgsz;
  56. nxtfree = mblklen;
  57. }
  58. /*
  59. * Allocate memory in large chunks to avoid space and time overhead of
  60. * malloc(). This is possible because memory allocated by xalloc()
  61. * need never to be freed.
  62. */
  63. void *
  64. xalloc(size_t sz)
  65. {
  66. void *ptr;
  67. int prot, flags;
  68. /* Align to at least 8 bytes. */
  69. sz = (sz + 7) & ~7L;
  70. if (nxtfree + sz > mblklen) {
  71. /* use mmap() instead of malloc() to avoid malloc overhead. */
  72. prot = PROT_READ | PROT_WRITE;
  73. flags = MAP_ANON | MAP_PRIVATE;
  74. mbuf = mmap(NULL, mblklen, prot, flags, -1, (off_t)0);
  75. if (mbuf == (void *)MAP_FAILED)
  76. err(1, "can't map memory");
  77. (void)memset(mbuf, 0, mblklen);
  78. nxtfree = 0;
  79. }
  80. ptr = (char *)mbuf + nxtfree;
  81. nxtfree += sz;
  82. return (ptr);
  83. }