fix a memory leak
[calc.git] / workspace.c
CommitLineData
e952523a
LM
1#include <malloc.h>
2#include <stdio.h>
3
4#include "argument.h"
5#include "debug.h"
6#include "element.h"
7#include "parser.h"
8#include "stack.h"
9#include "storage.h"
10#include "tabular.h"
11
12#include "workspace.h"
13
14/* allocate workspace*/
15
16workspace_t *alloc_ws ()
17{
18 return (workspace_t *) callocordie (1, sizeof (workspace_t));
19}
20
21/* backup workspace*/
22
23workspace_t *backup_ws (workspace_t *ws)
24{
25 ws->answer = answer;
a3f2e6cf
LM
26 if (ws->argument) {
27 free_tab (ws->argument);
28 }
e952523a 29 ws->argument = copy_tab (argument);
a3f2e6cf
LM
30 if (ws->stack) {
31 free_tab (ws->stack);
32 }
e952523a 33 ws->stack = copy_tab (stack);
a3f2e6cf
LM
34 if (ws->storage) {
35 free_tab (ws->storage);
36 }
e952523a
LM
37 ws->storage = copy_tab (storage);
38 return ws;
39}
40
41/* clean workspace */
42
43workspace_t *clean_ws (workspace_t *ws)
44{
45 ws->answer = 0;
46
47 if (ws->argument) {
48 free_tab (ws->argument);
49 ws->argument = NULL;
50 }
51
52 ws->id = 0;
53
54 if (ws->root) {
55 delelement (ws->root);
56 ws->root = NULL;
57 }
58
59 if (ws->stack) {
60 free_tab (ws->stack);
61 ws->stack = NULL;
62 }
63
64 if (ws->string) {
65 free (ws->string);
66 ws->string = NULL;
67 }
68
69 if (ws->storage) {
70 free_tab (ws->storage);
71 ws->storage = NULL;
72 }
73
74 return ws;
75}
76
77/* free workspace */
78
79void free_ws (workspace_t *ws)
80{
81 if (ws) {
82 clean_ws (ws);
83 free (ws);
84 }
85}
86
87/* restore workspace*/
88
89void restore_ws (workspace_t *ws)
90{
91 answer = ws->answer;
92
93 if (argument) {
94 free_tab (argument);
95 }
96 argument = copy_tab (ws->argument);
97
98 if (stack) {
99 free_tab (stack);
100 }
101 stack = copy_tab (ws->stack);
102
103 if (storage) {
104 free_tab (storage);
105 }
106 storage = copy_tab (ws->storage);
107}
108
109/* vim: set ts=4 sw=4 et: */