Previous 15994 Revisions Next

r6077 Wednesday 14th October, 2009 at 12:48:11 UTC by Dirk Best
Move pile.c/h to tools directory, some makefile cleanups
[src/mess]mess.mak messcore.mak pile.c pile.h
[src/mess/tools]pile.c* pile.h* tools.mak*
[src/mess/tools/imgtool/windows]wimgtool.mak
[src/mess/tools/messtest]messtest.mak

src/mess/pile.h
r6076r6077
1/****************************************************************************
2
3   pile.h
4
5   Utility code to implement "piles"; blocks of data that are built up
6   and then detached
7
8****************************************************************************/
9
10#ifndef PILE_H
11#define PILE_H
12
13#include <stdlib.h>
14#include <stdarg.h>
15
16typedef struct
17{
18   void *ptr;
19   size_t physical_size;
20   size_t logical_size;
21} mess_pile;
22
23void pile_init(mess_pile *pile);
24void pile_delete(mess_pile *pile);
25void pile_clear(mess_pile *pile);
26void *pile_detach(mess_pile *pile);
27int pile_write(mess_pile *pile, const void *ptr, size_t size);
28int pile_writebyte(mess_pile *pile, char byte, size_t size);
29
30INLINE void *pile_getptr(mess_pile *pile)
31{
32   return pile->ptr;
33}
34
35INLINE size_t pile_size(mess_pile *pile)
36{
37   return pile->logical_size;
38}
39
40int pile_vprintf(mess_pile *pile, const char *fmt, va_list args);
41#ifdef __GNUC__
42int pile_printf(mess_pile *pile, const char *fmt, ...)/* ATTR_PRINTF(2,3)*/__attribute__((format(printf, 2, 3)));
43#else
44int pile_printf(mess_pile *pile, const char *fmt, ...)/* ATTR_PRINTF(2,3)*/;
45#endif
46int pile_putc(mess_pile *pile, char c);
47int pile_puts(mess_pile *pile, const char *s);
48
49#endif /* PILE_H */
50
51
src/mess/pile.c
r6076r6077
1/****************************************************************************
2
3   pile.h
4
5   Utility code to implement "piles"; blocks of data that are built up
6   and then detached
7
8****************************************************************************/
9
10#include <string.h>
11#include <stdio.h>
12
13#include "pile.h"
14
15void pile_init(mess_pile *pile)
16{
17   pile->ptr = NULL;
18   pile->physical_size = 0;
19   pile->logical_size = 0;
20}
21
22
23
24void pile_delete(mess_pile *pile)
25{
26   if (pile->ptr)
27   {
28      free(pile->ptr);
29      pile_init(pile);
30   }
31}
32
33
34
35void pile_clear(mess_pile *pile)
36{
37   pile_delete(pile);
38}
39
40
41
42void *pile_detach(mess_pile *pile)
43{
44   void *ptr;
45
46   ptr = realloc(pile->ptr, pile->logical_size);
47   if (!ptr)
48      ptr = pile->ptr;
49
50   pile_init(pile);
51   return ptr;
52}
53
54
55
56int pile_write(mess_pile *pile, const void *ptr, size_t size)
57{
58   size_t new_logical_size;
59   size_t new_physical_size;
60   size_t blocksize = 4096;
61   char *new_ptr;
62
63   new_logical_size = pile->logical_size + size;
64   if (new_logical_size > pile->physical_size)
65   {
66      new_physical_size = new_logical_size + blocksize;
67      new_physical_size -= new_physical_size % blocksize;
68
69      new_ptr = (char *) realloc(pile->ptr, new_physical_size);
70      if (!new_ptr)
71         return -1;
72
73      pile->ptr = new_ptr;
74      pile->physical_size = new_physical_size;
75   }
76   memcpy(((char *) pile->ptr) + pile->logical_size, ptr, size);
77   pile->logical_size = new_logical_size;
78   return 0;
79}
80
81
82
83int pile_writebyte(mess_pile *pile, char byte, size_t size)
84{
85   int rc = 0;
86
87   while(size--)
88   {
89      rc = pile_write(pile, &byte, 1);
90      if (rc)
91         return rc;
92   }
93   return rc;
94}
95
96
97
98int pile_vprintf(mess_pile *pile, const char *fmt, va_list args)
99{
100   int count;
101   char buf[1024];
102
103   count = vsnprintf(buf, sizeof(buf) / sizeof(buf[0]), fmt, args);
104   if (pile_write(pile, buf, count * sizeof(*buf)))
105      return 0;
106   return count;
107}
108
109
110
111int pile_printf(mess_pile *pile, const char *fmt, ...)
112{
113   int count;
114   va_list va;
115
116   va_start(va, fmt);
117   count = pile_vprintf(pile, fmt, va);
118   va_end(va);
119   return count;
120}
121
122
123
124int pile_putc(mess_pile *pile, char c)
125{
126   return pile_write(pile, &c, sizeof(c));
127}
128
129
130
131int pile_puts(mess_pile *pile, const char *s)
132{
133   size_t size;
134   size = strlen(s);
135   return pile_write(pile, s, size);
136}
137
138
src/mess/messcore.mak
r6076r6077
4646MESS_LAYOUT = $(MESSOBJ)/layout
4747MESS_MACHINE = $(MESSOBJ)/machine
4848MESS_VIDEO = $(MESSOBJ)/video
49MESS_TOOLS = $(MESSOBJ)/tools
5049
5150
5251OBJDIRS += \
src/mess/tools/pile.h
r0r6077
1/****************************************************************************
2
3   pile.h
4
5   Utility code to implement "piles"; blocks of data that are built up
6   and then detached
7
8****************************************************************************/
9
10#ifndef PILE_H
11#define PILE_H
12
13#include <stdlib.h>
14#include <stdarg.h>
15
16typedef struct
17{
18   void *ptr;
19   size_t physical_size;
20   size_t logical_size;
21} mess_pile;
22
23void pile_init(mess_pile *pile);
24void pile_delete(mess_pile *pile);
25void pile_clear(mess_pile *pile);
26void *pile_detach(mess_pile *pile);
27int pile_write(mess_pile *pile, const void *ptr, size_t size);
28int pile_writebyte(mess_pile *pile, char byte, size_t size);
29
30INLINE void *pile_getptr(mess_pile *pile)
31{
32   return pile->ptr;
33}
34
35INLINE size_t pile_size(mess_pile *pile)
36{
37   return pile->logical_size;
38}
39
40int pile_vprintf(mess_pile *pile, const char *fmt, va_list args);
41#ifdef __GNUC__
42int pile_printf(mess_pile *pile, const char *fmt, ...)/* ATTR_PRINTF(2,3)*/__attribute__((format(printf, 2, 3)));
43#else
44int pile_printf(mess_pile *pile, const char *fmt, ...)/* ATTR_PRINTF(2,3)*/;
45#endif
46int pile_putc(mess_pile *pile, char c);
47int pile_puts(mess_pile *pile, const char *s);
48
49#endif /* PILE_H */
50
51
src/mess/tools/messtest/messtest.mak
r6076r6077
2323OBJDIRS += $(MESSTESTOBJ)
2424
2525MESSTEST_OBJS =   \
26   $(MESSOBJ)/pile.o         \
26   $(MESS_TOOLS)/pile.o      \
2727   $(MESSTESTOBJ)/main.o      \
2828   $(MESSTESTOBJ)/core.o      \
2929   $(MESSTESTOBJ)/testmess.o   \
src/mess/tools/tools.mak
r0r6077
1###########################################################################
2#
3#   tools.mak
4#
5#   MESS tools makefile
6#
7###########################################################################
8 
9 
10# add include path to tools directory
11CFLAGS += -I$(MESSSRC)/tools
12 
13# tools object directory
14MESS_TOOLS = $(MESSOBJ)/tools
15 
16 
17ifdef BUILD_IMGTOOL
18include $(MESSSRC)/tools/imgtool/imgtool.mak
19TOOLS += $(IMGTOOL)
20endif
21 
22ifdef BUILD_CASTOOL
23include $(MESSSRC)/tools/castool/castool.mak
24TOOLS += $(CASTOOL)
25endif
26 
27ifdef BUILD_MESSTEST
28include $(MESSSRC)/tools/messtest/messtest.mak
29TOOLS += $(MESSTEST)
30endif
31 
32ifdef BUILD_DAT2HTML
33include $(MESSSRC)/tools/dat2html/dat2html.mak
34TOOLS += $(DAT2HTML)
35endif
36 
37# include OS-specific MESS stuff
38ifeq ($(OSD),windows)
39include $(MESSSRC)/tools/messdocs/messdocs.mak
40 
41ifdef BUILD_WIMGTOOL
42include $(MESSSRC)/tools/imgtool/windows/wimgtool.mak
43TOOLS += $(WIMGTOOL)
44endif
45endif
src/mess/tools/imgtool/windows/wimgtool.mak
r6076r6077
2323OBJDIRS += $(WIMGTOOLOBJ)
2424
2525WIMGTOOL_OBJS = \
26   $(MESSOBJ)/pile.o                     \
26   $(MESS_TOOLS)/pile.o                  \
2727   $(MESS_TOOLS)/toolerr.o                  \
2828   $(MESSOBJ)/osd/windows/opcntrl.o         \
2929   $(MESSOBJ)/osd/windows/winutils.o         \
src/mess/tools/pile.c
r0r6077
1/****************************************************************************
2
3   pile.h
4
5   Utility code to implement "piles"; blocks of data that are built up
6   and then detached
7
8****************************************************************************/
9
10#include <string.h>
11#include <stdio.h>
12
13#include "pile.h"
14
15void pile_init(mess_pile *pile)
16{
17   pile->ptr = NULL;
18   pile->physical_size = 0;
19   pile->logical_size = 0;
20}
21
22
23
24void pile_delete(mess_pile *pile)
25{
26   if (pile->ptr)
27   {
28      free(pile->ptr);
29      pile_init(pile);
30   }
31}
32
33
34
35void pile_clear(mess_pile *pile)
36{
37   pile_delete(pile);
38}
39
40
41
42void *pile_detach(mess_pile *pile)
43{
44   void *ptr;
45
46   ptr = realloc(pile->ptr, pile->logical_size);
47   if (!ptr)
48      ptr = pile->ptr;
49
50   pile_init(pile);
51   return ptr;
52}
53
54
55
56int pile_write(mess_pile *pile, const void *ptr, size_t size)
57{
58   size_t new_logical_size;
59   size_t new_physical_size;
60   size_t blocksize = 4096;
61   char *new_ptr;
62
63   new_logical_size = pile->logical_size + size;
64   if (new_logical_size > pile->physical_size)
65   {
66      new_physical_size = new_logical_size + blocksize;
67      new_physical_size -= new_physical_size % blocksize;
68
69      new_ptr = (char *) realloc(pile->ptr, new_physical_size);
70      if (!new_ptr)
71         return -1;
72
73      pile->ptr = new_ptr;
74      pile->physical_size = new_physical_size;
75   }
76   memcpy(((char *) pile->ptr) + pile->logical_size, ptr, size);
77   pile->logical_size = new_logical_size;
78   return 0;
79}
80
81
82
83int pile_writebyte(mess_pile *pile, char byte, size_t size)
84{
85   int rc = 0;
86
87   while(size--)
88   {
89      rc = pile_write(pile, &byte, 1);
90      if (rc)
91         return rc;
92   }
93   return rc;
94}
95
96
97
98int pile_vprintf(mess_pile *pile, const char *fmt, va_list args)
99{
100   int count;
101   char buf[1024];
102
103   count = vsnprintf(buf, sizeof(buf) / sizeof(buf[0]), fmt, args);
104   if (pile_write(pile, buf, count * sizeof(*buf)))
105      return 0;
106   return count;
107}
108
109
110
111int pile_printf(mess_pile *pile, const char *fmt, ...)
112{
113   int count;
114   va_list va;
115
116   va_start(va, fmt);
117   count = pile_vprintf(pile, fmt, va);
118   va_end(va);
119   return count;
120}
121
122
123
124int pile_putc(mess_pile *pile, char c)
125{
126   return pile_write(pile, &c, sizeof(c));
127}
128
129
130
131int pile_puts(mess_pile *pile, const char *s)
132{
133   size_t size;
134   size = strlen(s);
135   return pile_write(pile, s, size);
136}
137
138
src/mess/mess.mak
r6076r6077
15311531$(MESS_MACHINE)/nes_mmc.o:    $(MESSSRC)/machine/nes_ines.c \
15321532            $(MESSSRC)/machine/nes_unif.c \
15331533
1534
15341535#-------------------------------------------------
15351536# layout dependencies
15361537#-------------------------------------------------
r6076r6077
15801581                     $(MESS_LAYOUT)/z80netb.lh \
15811582                     $(MESS_LAYOUT)/z80netf.lh
15821583
1584
15831585#-------------------------------------------------
15841586# MESS-specific tools
15851587#-------------------------------------------------
15861588
1587ifdef BUILD_IMGTOOL
1588include $(MESSSRC)/tools/imgtool/imgtool.mak
1589TOOLS += $(IMGTOOL)
1590endif
1589include $(MESSSRC)/tools/tools.mak
15911590
1592ifdef BUILD_CASTOOL
1593include $(MESSSRC)/tools/castool/castool.mak
1594TOOLS += $(CASTOOL)
1595endif
15961591
1597ifdef BUILD_MESSTEST
1598include $(MESSSRC)/tools/messtest/messtest.mak
1599TOOLS += $(MESSTEST)
1600endif
1601
1602ifdef BUILD_DAT2HTML
1603include $(MESSSRC)/tools/dat2html/dat2html.mak
1604TOOLS += $(DAT2HTML)
1605endif
1606
1607# include OS-specific MESS stuff
1608ifeq ($(OSD),windows)
1609include $(MESSSRC)/tools/messdocs/messdocs.mak
1610
1611ifdef BUILD_WIMGTOOL
1612include $(MESSSRC)/tools/imgtool/windows/wimgtool.mak
1613TOOLS += $(WIMGTOOL)
1614endif
1615endif
1616
1617
1618
16191592#-------------------------------------------------
16201593# MESS special OSD rules
16211594#-------------------------------------------------
16221595
1623include $(SRC)/mess/osd/$(OSD)/$(OSD).mak
1596include $(MESSSRC)/osd/$(OSD)/$(OSD).mak

Previous 15994 Revisions Next


© 1998-2018 The MESS Team