Skip to content

Commit 4455799

Browse files
author
Charlie Gordon
committed
improve consistency in new/delete methodology
* use qe_mallocz for all structures * prevent spurious uses of malloc/free/realloc * pass pointers to pointers to all deletion functions * use consistent patterns for list deletions * add more dependencies in Makefiles
1 parent 0308fae commit 4455799

28 files changed

+343
-325
lines changed

Makefile

+23-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# QEmacs, tiny but powerful multimode editor
22
#
33
# Copyright (c) 2000-2002 Fabrice Bellard.
4-
# Copyright (c) 2000-2013 Charlie Gordon.
4+
# Copyright (c) 2000-2014 Charlie Gordon.
55
#
66
# This library is free software; you can redistribute it and/or
77
# modify it under the terms of the GNU Lesser General Public
@@ -17,7 +17,9 @@
1717
# License along with this library; if not, write to the Free Software
1818
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1919

20-
include config.mak
20+
DEPTH=.
21+
22+
include $(DEPTH)/config.mak
2123

2224
ifeq ($(CC),gcc)
2325
CFLAGS += -Wall -g -O2 -funsigned-char
@@ -27,11 +29,11 @@ ifeq ($(CC),gcc)
2729
endif
2830

2931
#include local compiler configuration file
30-
-include cflags.mk
32+
-include $(DEPTH)/cflags.mk
3133

3234
ifdef TARGET_GPROF
33-
CFLAGS += -p
34-
LDFLAGS += -p
35+
CFLAGS += -p
36+
LDFLAGS += -p
3537
endif
3638

3739
TLDFLAGS := $(LDFLAGS)
@@ -125,19 +127,19 @@ endif
125127
ifdef CONFIG_X11
126128
OBJS+= x11.o
127129
ifdef CONFIG_XRENDER
128-
LIBS+=-lXrender
130+
LIBS+= -lXrender
129131
endif
130132
ifdef CONFIG_XV
131-
LIBS+=-lXv
133+
LIBS+= -lXv
132134
endif
133135
LIBS+= -L/usr/X11R6/lib -lXext -lX11
134136
endif
135137

136138
ifdef CONFIG_HTML
137-
CFLAGS+=-I./libqhtml
138-
DEP_LIBS+=libqhtml/libqhtml.a
139-
LIBS+=-L./libqhtml -lqhtml
140-
OBJS+=html.o docbook.o
139+
CFLAGS+= -I./libqhtml
140+
DEP_LIBS+= libqhtml/libqhtml.a
141+
LIBS+= -L./libqhtml -lqhtml
142+
OBJS+= html.o docbook.o
141143
ifndef CONFIG_WIN32
142144
TARGETLIBS+= libqhtml
143145
TARGETS+= html2png$(EXE)
@@ -162,9 +164,12 @@ SRCS:= $(OBJS:.o=.c)
162164
TSRCS:= $(TOBJS:.o=.c)
163165
TSRCS:= $(TSRCS:tqe.c=qe.c)
164166

165-
OBJS_DIR:=.objs
166-
OBJS:=$(addprefix $(OBJS_DIR)/, $(OBJS))
167-
TOBJS:=$(addprefix $(OBJS_DIR)/, $(TOBJS))
167+
DEPENDS:= qe.h config.h cutils.h display.h qestyles.h config.mak
168+
DEPENDS:= $(addprefix $(DEPTH)/, $(DEPENDS))
169+
170+
OBJS_DIR:= $(DEPTH)/.objs
171+
OBJS:= $(addprefix $(OBJS_DIR)/, $(OBJS))
172+
TOBJS:= $(addprefix $(OBJS_DIR)/, $(TOBJS))
168173

169174
$(shell mkdir -p $(OBJS_DIR))
170175

@@ -201,7 +206,7 @@ tqe$(EXE): tqe_g$(EXE) Makefile
201206
echo `size $@` `wc -c $@` tqe $(OPTIONS) \
202207
| cut -d ' ' -f 7-10,13,15-40 >> STATS
203208

204-
$(OBJS_DIR)/tqe.o: qe.c qe.h qestyles.h qeconfig.h config.h config.mak Makefile
209+
$(OBJS_DIR)/tqe.o: qe.c qeconfig.h $(DEPENDS) Makefile
205210
$(CC) $(DEFINES) -DCONFIG_TINY $(CFLAGS) -o $@ -c $<
206211

207212
ffplay$(EXE): qe$(EXE) Makefile
@@ -228,13 +233,13 @@ $(OBJS_DIR)/fbfrender.o: fbfrender.c fbfrender.h libfbf.h
228233
$(OBJS_DIR)/qe.o: qe.c qe.h qfribidi.h qeconfig.h
229234
$(OBJS_DIR)/qfribidi.o: qfribidi.c qfribidi.h
230235

231-
$(OBJS_DIR)/%.o: %.c qe.h qestyles.h config.h config.mak Makefile
236+
$(OBJS_DIR)/%.o: %.c $(DEPENDS) Makefile
232237
$(CC) $(DEFINES) $(CFLAGS) -o $@ -c $<
233238

234-
$(OBJS_DIR)/haiku.o: haiku.cpp qe.h qestyles.h config.h config.mak Makefile
239+
$(OBJS_DIR)/haiku.o: haiku.cpp $(DEPENDS) Makefile
235240
g++ $(DEFINES) $(CFLAGS) -Wno-multichar -o $@ -c $<
236241

237-
%.s: %.c qe.h qestyles.h config.h config.mak Makefile
242+
%.s: %.c $(DEPENDS) Makefile
238243
$(CC) $(DEFINES) $(CFLAGS) -o $@ -S $<
239244

240245
#

buffer.c

+42-49
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Buffer handling for QEmacs
33
*
4-
* Copyright (c) 2000 Fabrice Bellard.
4+
* Copyright (c) 2000-2002 Fabrice Bellard.
55
* Copyright (c) 2002-2014 Charlie Gordon.
66
*
77
* This library is free software; you can redistribute it and/or
@@ -435,14 +435,11 @@ void eb_delete(EditBuffer *b, int offset, int size)
435435
/* flush the log */
436436
void log_reset(EditBuffer *b)
437437
{
438-
if (b->log_buffer) {
439-
eb_free(b->log_buffer);
440-
b->log_buffer = NULL;
441-
b->log_new_index = 0;
442-
b->log_current = 0;
443-
b->nb_logs = 0;
444-
}
445-
b->modified = 0;
438+
eb_free(&b->log_buffer);
439+
b->log_new_index = 0;
440+
b->log_current = 0;
441+
b->nb_logs = 0;
442+
b->modified = 0; /* ??? */
446443
}
447444

448445
/* rename a buffer and add characters so that the name is unique */
@@ -548,44 +545,42 @@ void eb_clear(EditBuffer *b)
548545
//memset(b, 0, offsetof(EditBuffer, remanent_area));
549546
}
550547

551-
void eb_free(EditBuffer *b)
548+
void eb_free(EditBuffer **bp)
552549
{
553-
QEmacsState *qs = &qe_state;
554-
EditBuffer **pb;
555-
EditBufferCallbackList *l, *l1;
550+
if (*bp) {
551+
EditBuffer *b = *bp;
552+
QEmacsState *qs = &qe_state;
553+
EditBuffer **pb;
556554

557-
if (b == NULL)
558-
return;
559-
560-
/* call user defined close */
561-
if (b->close)
562-
b->close(b);
555+
/* call user defined close */
556+
if (b->close)
557+
b->close(b);
563558

564-
/* free each callback */
565-
for (l = b->first_callback; l != NULL;) {
566-
l1 = l->next;
567-
qe_free(&l);
568-
l = l1;
569-
}
570-
b->first_callback = NULL;
559+
/* free each callback */
560+
while (b->first_callback) {
561+
EditBufferCallbackList *cb = b->first_callback;
562+
b->first_callback = cb->next;
563+
qe_free(&cb);
564+
}
571565

572-
eb_clear(b);
566+
eb_clear(b);
573567

574-
/* suppress from buffer list */
575-
pb = &qs->first_buffer;
576-
while (*pb != NULL) {
577-
if (*pb == b)
578-
break;
579-
pb = &(*pb)->next;
580-
}
581-
*pb = (*pb)->next;
568+
/* suppress from buffer list */
569+
pb = &qs->first_buffer;
570+
while (*pb != NULL) {
571+
if (*pb == b)
572+
break;
573+
pb = &(*pb)->next;
574+
}
575+
*pb = (*pb)->next;
582576

583-
if (b == qs->trace_buffer)
584-
qs->trace_buffer = NULL;
577+
if (b == qs->trace_buffer)
578+
qs->trace_buffer = NULL;
585579

586-
eb_free_style_buffer(b);
580+
eb_free_style_buffer(b);
587581

588-
qe_free(&b);
582+
qe_free(bp);
583+
}
589584
}
590585

591586
EditBuffer *eb_find(const char *name)
@@ -702,11 +697,12 @@ void eb_trace_bytes(const void *buf, int size, int state)
702697
/************************************************************/
703698
/* callbacks */
704699

705-
int eb_add_callback(EditBuffer *b, EditBufferCallback cb, void *opaque, int arg)
700+
int eb_add_callback(EditBuffer *b, EditBufferCallback cb,
701+
void *opaque, int arg)
706702
{
707703
EditBufferCallbackList *l;
708704

709-
l = qe_malloc(EditBufferCallbackList);
705+
l = qe_mallocz(EditBufferCallbackList);
710706
if (!l)
711707
return -1;
712708
l->callback = cb;
@@ -775,10 +771,7 @@ int eb_create_style_buffer(EditBuffer *b, int flags)
775771

776772
void eb_free_style_buffer(EditBuffer *b)
777773
{
778-
if (b->b_styles) {
779-
eb_free(b->b_styles);
780-
b->b_styles = NULL;
781-
}
774+
eb_free(&b->b_styles);
782775
b->style_shift = b->style_bytes = 0;
783776
eb_free_callback(b, eb_style_callback, NULL);
784777
}
@@ -1488,7 +1481,7 @@ int load_buffer(EditBuffer *b, const char *filename,
14881481
/* cannot load a buffer if already I/Os or readonly */
14891482
if (b->flags & (BF_LOADING | BF_SAVING | BF_READONLY))
14901483
return -1;
1491-
s = qe_malloc(BufferIOState);
1484+
s = qe_mallocz(BufferIOState);
14921485
if (!s)
14931486
return -1;
14941487
b->io_state = s;
@@ -1804,7 +1797,7 @@ int eb_printf(EditBuffer *b, const char *fmt, ...)
18041797
va_start(ap, fmt);
18051798
size = len + 1;
18061799
#ifdef CONFIG_WIN32
1807-
buf = malloc(size);
1800+
buf = qe_malloc_bytes(size);
18081801
#else
18091802
buf = alloca(size);
18101803
#endif
@@ -1818,7 +1811,7 @@ int eb_printf(EditBuffer *b, const char *fmt, ...)
18181811
eb_insert_utf8_buf(b, b->total_size, buf, len);
18191812
#ifdef CONFIG_WIN32
18201813
if (buf != buf0)
1821-
free(buf);
1814+
qe_free(&buf);
18221815
#endif
18231816
return len;
18241817
}
@@ -1906,7 +1899,7 @@ int eb_insert_buffer_convert(EditBuffer *dest, int dest_offset,
19061899

19071900
if (b != dest) {
19081901
size = eb_insert_buffer(dest, dest_offset, b, 0, b->total_size);
1909-
eb_free(b);
1902+
eb_free(&b);
19101903
}
19111904
return size;
19121905
}

dired.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ static void dired_build_list(EditState *s, const char *path,
366366
item->opaque = dip;
367367
}
368368
}
369-
find_file_close(ffst);
369+
find_file_close(&ffst);
370370

371371
dired_sort_list(s);
372372

display.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static QEFont *dummy_dpy_open_font(__unused__ QEditScreen *s,
5858
}
5959

6060
static void dummy_dpy_close_font(__unused__ QEditScreen *s,
61-
__unused__ QEFont *font)
61+
__unused__ QEFont **fontp)
6262
{
6363
}
6464

@@ -277,8 +277,7 @@ QEFont *select_font(QEditScreen *s, int style, int size)
277277
goto fail;
278278
}
279279
if (font_cache[min_index]) {
280-
close_font(s, font_cache[min_index]);
281-
font_cache[min_index] = NULL;
280+
close_font(s, &font_cache[min_index]);
282281
}
283282
fc = open_font(s, style, size);
284283
if (!fc) {
@@ -313,7 +312,7 @@ QEBitmap *bmp_alloc(QEditScreen *s, int width, int height, int flags)
313312

314313
if (!s->dpy.dpy_bmp_alloc)
315314
return NULL;
316-
b = qe_malloc(QEBitmap);
315+
b = qe_mallocz(QEBitmap);
317316
if (!b)
318317
return NULL;
319318
b->width = width;

display.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ struct QEDisplay {
105105
void (*dpy_fill_rectangle)(QEditScreen *s,
106106
int x, int y, int w, int h, QEColor color);
107107
QEFont *(*dpy_open_font)(QEditScreen *s, int style, int size);
108-
void (*dpy_close_font)(QEditScreen *s, QEFont *font);
108+
void (*dpy_close_font)(QEditScreen *s, QEFont **fontp);
109109
void (*dpy_text_metrics)(QEditScreen *s, QEFont *font,
110110
QECharMetrics *metrics,
111111
const unsigned int *str, int len);
@@ -171,10 +171,10 @@ static inline QEFont *open_font(QEditScreen *s,
171171
return s->dpy.dpy_open_font(s, style, size);
172172
}
173173

174-
static inline void close_font(QEditScreen *s, QEFont *font)
174+
static inline void close_font(QEditScreen *s, QEFont **fontp)
175175
{
176-
if (!font->system_font)
177-
s->dpy.dpy_close_font(s, font);
176+
if (*fontp && !(*fontp)->system_font)
177+
s->dpy.dpy_close_font(s, fontp);
178178
}
179179

180180
static inline void text_metrics(QEditScreen *s, QEFont *font,

extras.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ static void do_transpose(EditState *s, int cmd)
412412
/* XXX: This will create 2 undo records */
413413
eb_delete(b, offset0, size0 + size1 + size2);
414414
eb_insert_buffer_convert(b, offset0, b1, 0, size0 + size1 + size2);
415-
eb_free(b1);
415+
eb_free(&b1);
416416
}
417417
s->offset = end_offset;
418418
}
@@ -592,7 +592,7 @@ void do_apropos(EditState *s, const char *str)
592592
}
593593
} else {
594594
if (show)
595-
eb_free(b);
595+
eb_free(&b);
596596
put_status(s, "No apropos matches for `%s'", str);
597597
}
598598
}

fbfrender.c

+6-8
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ QEFont *fbf_open_font(__unused__ QEditScreen *s, int style, int size)
237237
UniFontData *fonts[MAX_MATCHES];
238238
int nb_fonts, i;
239239

240-
font = qe_malloc(QEFont);
240+
font = qe_mallocz(QEFont);
241241
if (!font)
242242
return NULL;
243243

@@ -276,9 +276,9 @@ QEFont *fbf_open_font(__unused__ QEditScreen *s, int style, int size)
276276
return font;
277277
}
278278

279-
void fbf_close_font(__unused__ QEditScreen *s, QEFont *font)
279+
void fbf_close_font(__unused__ QEditScreen *s, QEFont **fontp)
280280
{
281-
qe_free(&font);
281+
qe_free(fontp);
282282
}
283283

284284
static void *my_malloc(__unused__ void *opaque, int size)
@@ -485,17 +485,15 @@ int fbf_render_init(__unused__ const char *font_path)
485485

486486
void fbf_render_cleanup(void)
487487
{
488-
UniFontData *uf, *uf1;
489-
490-
for (uf = first_font; uf != NULL; uf = uf1) {
491-
uf1 = uf->next_font;
488+
while (first_font) {
489+
UniFontData *uf = first_font;
490+
first_font = uf->next_font;
492491
/* close font data structures */
493492
fbf_free_font(uf);
494493
/* close font file */
495494
qe_free(&uf->infile);
496495
qe_free(&uf);
497496
}
498-
first_font = NULL;
499497
}
500498

501499
#endif

fbfrender.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ void fbf_text_metrics(QEditScreen *s, QEFont *font,
4343
const unsigned int *str, int len);
4444
GlyphCache *decode_cached_glyph(QEditScreen *s, QEFont *font, int code);
4545
QEFont *fbf_open_font(QEditScreen *s, int style, int size);
46-
void fbf_close_font(QEditScreen *s, QEFont *font);
46+
void fbf_close_font(QEditScreen *s, QEFont **fontp);
4747

4848
int fbf_render_init(const char *font_path);
4949
void fbf_render_cleanup(void);

0 commit comments

Comments
 (0)