1f2c50142SMatthew Dillon /*
2f2c50142SMatthew Dillon * Copyright (c) 2006 The DragonFly Project. All rights reserved.
3f2c50142SMatthew Dillon *
4f2c50142SMatthew Dillon * This code is derived from software contributed to The DragonFly Project
5f2c50142SMatthew Dillon * by Matthew Dillon <dillon@backplane.com>
6f2c50142SMatthew Dillon *
7f2c50142SMatthew Dillon * Redistribution and use in source and binary forms, with or without
8f2c50142SMatthew Dillon * modification, are permitted provided that the following conditions
9f2c50142SMatthew Dillon * are met:
10f2c50142SMatthew Dillon *
11f2c50142SMatthew Dillon * 1. Redistributions of source code must retain the above copyright
12f2c50142SMatthew Dillon * notice, this list of conditions and the following disclaimer.
13f2c50142SMatthew Dillon * 2. Redistributions in binary form must reproduce the above copyright
14f2c50142SMatthew Dillon * notice, this list of conditions and the following disclaimer in
15f2c50142SMatthew Dillon * the documentation and/or other materials provided with the
16f2c50142SMatthew Dillon * distribution.
17f2c50142SMatthew Dillon * 3. Neither the name of The DragonFly Project nor the names of its
18f2c50142SMatthew Dillon * contributors may be used to endorse or promote products derived
19f2c50142SMatthew Dillon * from this software without specific, prior written permission.
20f2c50142SMatthew Dillon *
21f2c50142SMatthew Dillon * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22f2c50142SMatthew Dillon * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23f2c50142SMatthew Dillon * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24f2c50142SMatthew Dillon * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25f2c50142SMatthew Dillon * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26f2c50142SMatthew Dillon * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27f2c50142SMatthew Dillon * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28f2c50142SMatthew Dillon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29f2c50142SMatthew Dillon * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30f2c50142SMatthew Dillon * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31f2c50142SMatthew Dillon * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32f2c50142SMatthew Dillon * SUCH DAMAGE.
33f2c50142SMatthew Dillon */
34f2c50142SMatthew Dillon
35f2c50142SMatthew Dillon #include <sys/types.h>
36f2c50142SMatthew Dillon #include <sys/mman.h>
37f2c50142SMatthew Dillon #include <stdio.h>
38f2c50142SMatthew Dillon #include <stdlib.h>
397bc7e232SSascha Wildner #include <string.h>
40f2c50142SMatthew Dillon #include <unistd.h>
41eace1f85SSascha Wildner #include "fsutil.h"
42f2c50142SMatthew Dillon #include "memzone.h"
43f2c50142SMatthew Dillon
44f2c50142SMatthew Dillon /*
45f2c50142SMatthew Dillon * Efficiently allocate memory that will only be freed in bulk
46f2c50142SMatthew Dillon */
47f2c50142SMatthew Dillon void *
mzalloc(struct memzone * zone,int bytes)48f2c50142SMatthew Dillon mzalloc(struct memzone *zone, int bytes)
49f2c50142SMatthew Dillon {
50f2c50142SMatthew Dillon struct memchunk *chunk;
51f2c50142SMatthew Dillon void *ptr;
52f2c50142SMatthew Dillon
53f2c50142SMatthew Dillon if ((chunk = zone->curr) != NULL) {
54f2c50142SMatthew Dillon if (bytes > chunk->bytes - zone->index) {
55f2c50142SMatthew Dillon chunk->next = zone->list;
56f2c50142SMatthew Dillon zone->list = chunk;
57f2c50142SMatthew Dillon zone->curr = NULL;
58f2c50142SMatthew Dillon chunk = NULL;
59f2c50142SMatthew Dillon }
60f2c50142SMatthew Dillon }
61f2c50142SMatthew Dillon if (chunk == NULL) {
62f2c50142SMatthew Dillon chunk = malloc(sizeof(*chunk));
63f2c50142SMatthew Dillon if (chunk == NULL)
64f2c50142SMatthew Dillon return(NULL);
65f2c50142SMatthew Dillon bzero(chunk, sizeof(*chunk));
66f2c50142SMatthew Dillon chunk->base = mmap(NULL, MEMZONE_CHUNK, PROT_READ|PROT_WRITE,
67f2c50142SMatthew Dillon MAP_ANON|MAP_PRIVATE, -1, 0);
68f2c50142SMatthew Dillon if (chunk->base == MAP_FAILED) {
69f2c50142SMatthew Dillon free(chunk);
70f2c50142SMatthew Dillon return(NULL);
71f2c50142SMatthew Dillon }
72f2c50142SMatthew Dillon chunk->bytes = MEMZONE_CHUNK;
73f2c50142SMatthew Dillon zone->curr = chunk;
74f2c50142SMatthew Dillon zone->index = 0;
75f2c50142SMatthew Dillon }
76f2c50142SMatthew Dillon if (bytes > chunk->bytes)
77f2c50142SMatthew Dillon pfatal("allocation to large for mzalloc!");
78f2c50142SMatthew Dillon ptr = chunk->base + zone->index;
79f2c50142SMatthew Dillon zone->index += (bytes + 7) & ~7;
80f2c50142SMatthew Dillon return(ptr);
81f2c50142SMatthew Dillon }
82f2c50142SMatthew Dillon
83f2c50142SMatthew Dillon /*
84f2c50142SMatthew Dillon * Free memory in bulk
85f2c50142SMatthew Dillon */
86*a3e9bf8fSSascha Wildner void
mzpurge(struct memzone * zone)87f2c50142SMatthew Dillon mzpurge(struct memzone *zone)
88f2c50142SMatthew Dillon {
89f2c50142SMatthew Dillon struct memchunk *chunk;
90f2c50142SMatthew Dillon
91f2c50142SMatthew Dillon if ((chunk = zone->curr) != NULL) {
92f2c50142SMatthew Dillon chunk->next = zone->list;
93f2c50142SMatthew Dillon zone->list = chunk;
94f2c50142SMatthew Dillon zone->curr = NULL;
95f2c50142SMatthew Dillon }
96f2c50142SMatthew Dillon while ((chunk = zone->list) != NULL) {
97f2c50142SMatthew Dillon zone->list = chunk->next;
98f2c50142SMatthew Dillon munmap(chunk->base, chunk->bytes);
99f2c50142SMatthew Dillon free(chunk);
100f2c50142SMatthew Dillon }
101f2c50142SMatthew Dillon }
102f2c50142SMatthew Dillon
103