// license:GPL-2.0+ // copyright-holders:Couriersud /* * palloc.c * */ #include #include "pconfig.h" #include "palloc.h" #include "pfmtlog.h" namespace plib { //============================================================ // Exceptions //============================================================ pexception::pexception(const pstring text) { m_text = text; } file_e::file_e(const pstring fmt, const pstring &filename) : pexception(pfmt(fmt)(filename)) { } file_open_e::file_open_e(const pstring &filename) : file_e("File open failed: {}", filename) { } file_read_e::file_read_e(const pstring &filename) : file_e("File read failed: {}", filename) { } file_write_e::file_write_e(const pstring &filename) : file_e("File write failed: {}", filename) { } null_argument_e::null_argument_e(const pstring &argument) : pexception(pfmt("Null argument passed: {}")(argument)) { } out_of_mem_e::out_of_mem_e(const pstring &location) : pexception(pfmt("Out of memory: {}")(location)) { } //============================================================ // Memory pool //============================================================ mempool::mempool(size_t min_alloc, size_t min_align) : m_min_alloc(min_alloc), m_min_align(min_align) { } mempool::~mempool() { for (auto & b : m_blocks) { if (b.m_num_alloc != 0) { fprintf(stderr, "Found block with %d dangling allocations\n", static_cast(b.m_num_alloc)); } delete b.data; } m_blocks.clear(); } size_t mempool::new_block() { block b; b.data = new char[m_min_alloc]; b.cur_ptr = b.data; b.m_free = m_min_alloc; b.m_num_alloc = 0; m_blocks.push_back(b); return m_blocks.size() - 1; } void *mempool::alloc(size_t size) { size_t rs = (size + sizeof(info) + m_min_align - 1) & ~(m_min_align - 1); for (size_t bn=0; bn < m_blocks.size(); bn++) { auto &b = m_blocks[bn]; if (b.m_free > rs) { b.m_free -= rs; b.m_num_alloc++; auto i = reinterpret_cast(b.cur_ptr); i->m_block = bn; auto ret = reinterpret_cast(b.cur_ptr + sizeof(info)); b.cur_ptr += rs; return ret; } } { size_t bn = new_block(); auto &b = m_blocks[bn]; b.m_num_alloc = 1; b.m_free = m_min_alloc - rs; auto i = reinterpret_cast(b.cur_ptr); i->m_block = bn; auto ret = reinterpret_cast(b.cur_ptr + sizeof(info)); b.cur_ptr += rs; return ret; } } void mempool::free(void *ptr) { auto p = reinterpret_cast(ptr); auto i = reinterpret_cast(p - sizeof(info)); block *b = &m_blocks[i->m_block]; if (b->m_num_alloc == 0) fprintf(stderr, "Argh .. double free\n"); else { //b->m_free = m_min_alloc; //b->cur_ptr = b->data; } b->m_num_alloc--; } } ptions'>x86_std-exceptions MAME - Multiple Arcade Machine Emulator
summaryrefslogtreecommitdiffstatshomepage
blob: 4db45ca0a90887ef749b086e4cf8593c43eb0dff (plain) (blame)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// license:BSD-3-Clause
// copyright-holders:Aaron Giles
/***************************************************************************

    rendfont.h

    Rendering system font management.

***************************************************************************/

#ifndef __RENDFONT_H__
#define __RENDFONT_H__

#include "render.h"

// forward instead of include
class osd_font;

//**************************************************************************
//  TYPE DEFINITIONS
//**************************************************************************


// ======================> render_font

// a render_font describes and provides an interface to a font
class render_font
{
	friend class render_manager;
	friend resource_pool_object<render_font>::~resource_pool_object();

	// construction/destruction
	render_font(render_manager &manager, const char *filename);
	virtual ~render_font();

public:
	// getters
	render_manager &manager() const { return m_manager; }

	// size queries
	INT32 pixel_height() const { return m_height; }
	float char_width(float height, float aspect, unicode_char ch);
	float string_width(float height, float aspect, const char *string);
	float utf8string_width(float height, float aspect, const char *utf8string);

	// texture/bitmap queries
	render_texture *get_char_texture_and_bounds(float height, float aspect, unicode_char ch, render_bounds &bounds);
	void get_scaled_bitmap_and_bounds(bitmap_argb32 &dest, float height, float aspect, unicode_char chnum, rectangle &bounds);

private:
	// a glyph describes a single glyph
	class glyph
	{
	public:
		glyph()
			: width(0),
				xoffs(0), yoffs(0),
				bmwidth(0), bmheight(0),
				rawdata(NULL),
				texture(NULL) { }

		INT32               width;              // width from this character to the next
		INT32               xoffs, yoffs;       // X and Y offset from baseline to top,left of bitmap
		INT32               bmwidth, bmheight;  // width and height of bitmap
		const char *        rawdata;            // pointer to the raw data for this one
		render_texture *    texture;            // pointer to a texture for rendering and sizing
		bitmap_argb32       bitmap;             // pointer to the bitmap containing the raw data
	};

	// internal format
	enum format
	{
		FF_UNKNOWN,
		FF_TEXT,
		FF_CACHED,
		FF_OSD
	};

	// helpers
	glyph &get_char(unicode_char chnum);
	void char_expand(unicode_char chnum, glyph &ch);
	bool load_cached_bdf(const char *filename);
	bool load_bdf();
	bool load_cached(emu_file &file, UINT32 hash);
	bool save_cached(const char *filename, UINT32 hash);

	// internal state
	render_manager &    m_manager;
	format              m_format;           // format of font data
	int                 m_height;           // height of the font, from ascent to descent
	int                 m_yoffs;            // y offset from baseline to descent
	float               m_scale;            // 1 / height precomputed
	dynamic_array<glyph> m_glyphs[256];     // array of glyph subtables
	dynamic_array<char> m_rawdata;          // pointer to the raw data for the font
	UINT64              m_rawsize;          // size of the raw font data
	osd_font            *m_osdfont;          // handle to the OSD font

	// constants
	static const int CACHED_CHAR_SIZE       = 12;
	static const int CACHED_HEADER_SIZE     = 16;
	static const int CACHED_BDF_HASH_SIZE   = 1024;
};


#endif  /* __RENDFONT_H__ */