Welcome to mirror list, hosted at ThFree Co, Russian Federation.

buffers.c « base « src - github.com/windirstat/premake-4.x-stable.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2e3304bb2ffe9aac77a79d091d8f12d6e8879550 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
 * \file   buffers.c
 * \brief  Shared working buffer system.
 * \author Copyright (c) 2007-2007 Jason Perkins and the Premake project
 * 
 * \note I need to do a lot of string building operations in Premake. Rather than
 * constantly creating, resizing, and releasing (and forgetting to release)
 * dynamic string buffers, I use this shared buffer pool instead. Each request
 * to buffer_next() returns the next buffer in the list. Pointers to the buffers 
 * can be safely passed around, and I don't need to remember to release anything 
 * when I'm done. The buffers should only be used for transient values, obviously.
 * If you need to keep a value around for any length of time copy it to a string.
 *
 * \note The size and number of the buffers is arbitrary; I just picked some numbers
 * that seemed big enough.
 */

#include <stdlib.h>
#include "premake.h"
#include "base/buffers.h"

/** The size of an individual buffer, in bytes. */
const int BUFFER_SIZE = 0x4000;

/** The number of buffers stored in the pool */
static const int NUM_BUFFERS = 128;

/** The pool of buffers */
static char** buffers = NULL;

/** The index of the next available buffer */
static int next = 0;


/**
 * Clean up after the buffer system. Called by atexit().
 */
static void buffers_destroy(void)
{
	if (buffers != NULL)
	{
		int i;
		for (i = 0; i < NUM_BUFFERS; ++i)
		{
			free(buffers[i]);
		}
		free(buffers);
	}
}


/**
 * Initialize the buffer system.
 */
static void buffers_create(void)
{
	int i;
	buffers = (char**)malloc(sizeof(char*) * NUM_BUFFERS);
	for (i = 0; i < NUM_BUFFERS; ++i)
	{
		buffers[i] = (char*)malloc(BUFFER_SIZE);
	}
	next = 0;
	atexit(buffers_destroy);
}


/**
 * Get a clean buffer.
 * \returns An empty buffer.
 */
char * buffers_next()
{
	/* if this is the first call, initialize the buffer system */
	if (buffers == NULL)
		buffers_create();

	next++;
	if (next == NUM_BUFFERS)
		next = 0;

	/* initialize new buffers to empty string */
	buffers[next][0] = '\0';
	return buffers[next];
}