From: Christian Heller Date: Fri, 30 Aug 2013 23:20:21 +0000 (+0200) Subject: Moved allocation and freeing of Win structs into windows library. X-Git-Tag: tce~994 X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/%7B%7Byoutube_prefix%7D%7D%7B%7Bvideo_id%7D%7D?a=commitdiff_plain;h=0399a31b31047ab82348f733d6c56524258d1188;p=plomrogue Moved allocation and freeing of Win structs into windows library. --- diff --git a/src/rexit.c b/src/rexit.c index 2049613..85e2bfc 100644 --- a/src/rexit.c +++ b/src/rexit.c @@ -10,8 +10,7 @@ #include "map.h" /* for Map struct */ #include "keybindings.h" /* for KeysWinData, KeyBinding structs */ #include "command_db.h" /* for free_command_db() */ -#include "windows.h" /* for Win struct */ -#include "wincontrol.h" /* for free_win() */ +#include "windows.h" /* for Win struct, free_win() */ diff --git a/src/wincontrol.c b/src/wincontrol.c index 793b75e..74c9c2a 100644 --- a/src/wincontrol.c +++ b/src/wincontrol.c @@ -15,14 +15,6 @@ -extern void free_win(struct Win * win) -{ - free(win->_title); - free(win); -} - - - extern struct Win * init_win_from_file(struct World * world, char * w_name, void (* f) (struct Win *)) { @@ -54,10 +46,10 @@ extern struct Win * init_win_from_file(struct World * world, char * w_name, exit_err(fclose(file), world, err); struct WinMeta * wmeta = world->wins.meta; - struct Win * w = malloc(sizeof(struct Win)); - init_win(wmeta, w, title, height, width, world, f); + struct Win * wp; // = malloc(sizeof(struct Win)); + init_win(wmeta, &wp, title, height, width, world, f); free(title); - return w; + return wp; } diff --git a/src/wincontrol.h b/src/wincontrol.h index cb7d0ba..29a6ddf 100644 --- a/src/wincontrol.h +++ b/src/wincontrol.h @@ -16,12 +16,6 @@ struct World; -/* Free allocated memory for an initialized Win struct. */ -extern void free_win(struct Win * win); - - - - /* Wrapper around init_win() that reads the desired window size and title from a * file at the path prefixing the provided win name "w_name" with * "config/windows/". "f"() is the window drawing function (Win._draw()). diff --git a/src/windows.c b/src/windows.c index bb78a8b..f75544b 100644 --- a/src/windows.c +++ b/src/windows.c @@ -388,10 +388,15 @@ extern uint8_t init_win_meta(WINDOW * screen, struct WinMeta * wmeta) -extern uint8_t init_win(struct WinMeta * wmeta, struct Win * w, char * title, +extern uint8_t init_win(struct WinMeta * wmeta, struct Win ** wp, char * title, int16_t height, int16_t width, void * data, void * func) { + struct Win * w = malloc(sizeof(struct Win)); + if (NULL == w) + { + return 1; + } w->_prev = 0; w->_next = 0; w->frame.curses_win = 0; @@ -427,11 +432,20 @@ extern uint8_t init_win(struct WinMeta * wmeta, struct Win * w, char * title, { w->frame.size.y = wmeta->padframe.size.y - 1; } + *wp = w; return 0; } +extern void free_win(struct Win * win) +{ + free(win->_title); + free(win); +} + + + extern uint8_t append_win(struct WinMeta * wmeta, struct Win * w) { if (0 != wmeta->_chain_start) diff --git a/src/windows.h b/src/windows.h index 0e3f79b..a40835d 100644 --- a/src/windows.h +++ b/src/windows.h @@ -113,12 +113,17 @@ extern uint8_t init_win_meta(WINDOW * screen, struct WinMeta * wmeta); * * Other members of the Win struct are initialized to 0. */ -extern uint8_t init_win(struct WinMeta * wmeta, struct Win * w, char * title, +extern uint8_t init_win(struct WinMeta * wmeta, struct Win ** w, char * title, int16_t height, int16_t width, void * data, void * func); +/* Free allocated memory for an initialized Win struct. */ +extern void free_win(struct Win * win); + + + /* Append/suspend window "w" to/from chain of visible windows below "wmeta". * Appended windows will become active. Suspended active windows will move the * active window selection to their successor in the window chain or, failing