add newTabTab.patch
[nedit-bw.git] / zero-based-Program-refcount.patch
blobf52b1b5256657aebe4211f7fa48193c16214a140
1 ---
3 source/interpret.c | 28 +++++++++++++++++++++-------
4 source/interpret.h | 2 ++
5 source/macro.c | 8 +++++---
6 source/parse.y | 3 ++-
7 source/smartIndent.c | 6 ++++--
8 5 files changed, 34 insertions(+), 13 deletions(-)
10 diff --quilt old/source/interpret.c new/source/interpret.c
11 --- old/source/interpret.c
12 +++ new/source/interpret.c
13 @@ -302,7 +302,7 @@ Program *FinishCreatingProgram(Accumulat
14 newProg->code = (Inst *)XtCalloc(progLen, sizeof(Inst));
15 memcpy(newProg->code, Prog, progLen * sizeof(Inst));
16 newProg->nested = False;
17 - newProg->refcount = 1;
18 + newProg->refcount = 0;
20 DISASM(newProg->name, newProg->code, ProgP - Prog);
22 @@ -314,12 +314,26 @@ Program *FinishCreatingProgram(Accumulat
24 void FreeProgram(Program *prog)
26 + if (prog->refcount > 0) {
27 + fprintf(stderr, "NEdit: warning freeing macro with positive refcount: %s\n",
28 + prog->name);
29 + }
30 + XtFree((char *)prog->code);
31 + XtFree((char *)prog);
34 +void PutProgram(Program *prog)
36 if (--prog->refcount == 0) {
37 - XtFree((char *)prog->code);
38 - XtFree((char *)prog);
39 + FreeProgram(prog);
43 +void GetProgram(Program *prog)
45 + prog->refcount++;
49 ** Add an operator (instruction) to the end of the current program
51 @@ -557,7 +571,8 @@ static int setupFrame(RestartData *conte
52 /* cached arg array */
53 *(context->stackP)++ = argArray;
55 - /* prog to free, if requested */
56 + /* frame holds reference to prog */
57 + GetProgram(prog);
58 context->stackP->tag = NO_TAG;
59 context->stackP->val.prog = prog;
60 context->stackP++;
61 @@ -635,7 +650,7 @@ static void rewindFrame(RestartData *con
63 context->frameP = newFrameP;
65 - FreeProgram(prog);
66 + PutProgram(prog);
68 context->pc = newPC;
70 @@ -688,7 +703,6 @@ int ExecuteMacro(WindowInfo *window, Pro
71 /* pre-set the return PC to NULL */
72 context->pc = NULL;
73 /* prog will be freed by cller, but by stack also, so inc refcount */
74 - prog->refcount++;
75 status = setupFrame(context, prog, nArgs, args, argArray,
76 prog->name ? prog->name : "<exec-macro>");
78 @@ -2721,7 +2735,6 @@ static int callSubroutineFromSymbol(Symb
80 if (sym->value.tag == MACRO_SUBR_TAG) {
81 prog = sym->value.val.prog;
82 - prog->refcount++;
83 if (prog->nested) {
84 return setupNestedFrame(Interpreter, prog);
86 @@ -2944,6 +2957,7 @@ int OverlayRoutineFromProg(Program *prog
88 Symbol sym;
90 + /* this is only a helper symbol without a ref to prog */
91 sym.attr = READONLY_ATTR;
92 sym.name = LookupString(prog->name, True);
93 sym.value.tag = MACRO_SUBR_TAG;
94 diff --quilt old/source/interpret.h new/source/interpret.h
95 --- old/source/interpret.h
96 +++ new/source/interpret.h
97 @@ -203,6 +203,8 @@ int AllocNStringCpy(NString *string, con
98 void GarbageCollectStrings(void);
99 void FreeRestartData(RestartData *context);
100 void FreeProgram(Program *prog);
101 +void PutProgram(Program *prog);
102 +void GetProgram(Program *prog);
103 void ModifyReturnedValue(RestartData *context, DataValue dv);
104 WindowInfo *MacroRunWindow(void);
105 WindowInfo *MacroFocusWindow(void);
106 diff --quilt old/source/macro.c new/source/macro.c
107 --- old/source/macro.c
108 +++ new/source/macro.c
109 @@ -721,6 +721,7 @@ static void runMacro(WindowInfo *window,
110 window->macroCmdData = cmdData;
111 cmdData->bannerIsUp = False;
112 cmdData->closeOnCompletion = False;
113 + GetProgram(prog);
114 cmdData->program = prog;
115 cmdData->context = NULL;
116 cmdData->continueWorkProcID = 0;
117 @@ -876,7 +877,7 @@ static void finishMacroCmdExecution(Wind
118 XtDestroyWidget(XtParent(cmdData->dialog));
120 /* Free execution information */
121 - FreeProgram(cmdData->program);
122 + PutProgram(cmdData->program);
123 XtFree((char *)cmdData);
124 window->macroCmdData = NULL;
126 @@ -3549,12 +3550,14 @@ static int defineMS(WindowInfo *window,
127 *errMsg = "Try to override a non macro function.";
128 return False;
130 - FreeProgram(sym->value.val.prog);
131 + GetProgram(prog);
132 + PutProgram(sym->value.val.prog);
133 sym->value.val.prog = prog;
135 else {
136 DataValue subrPtr;
137 subrPtr.tag = MACRO_SUBR_TAG;
138 + GetProgram(prog);
139 subrPtr.val.prog = prog;
140 sym = InstallSymbol(name, GLOBAL_SCOPE, NO_ATTR, subrPtr);
142 @@ -3885,7 +3888,6 @@ static int evalMS(WindowInfo *window, Da
143 XtFree(macronl);
145 prog->nested = True;
146 - prog->refcount = 0;
148 ret = OverlayRoutineFromProg(prog, nArgs, nArgs);
149 if (!ret) {
150 diff --quilt old/source/parse.y new/source/parse.y
151 --- old/source/parse.y
152 +++ new/source/parse.y
153 @@ -218,7 +218,7 @@ define: definekw blank definesym bla
154 yyerror("try to override built-in subroutine");
155 YYERROR;
157 - FreeProgram(sym->value.val.prog);
158 + PutProgram(sym->value.val.prog);
160 else {
161 DataValue subrPtr;
162 @@ -226,6 +226,7 @@ define: definekw blank definesym bla
163 sym = InstallSymbol(prog->name, GLOBAL_SCOPE, READONLY_ATTR,
164 subrPtr);
166 + GetProgram(prog);
167 sym->value.val.prog = prog;
170 diff --quilt old/source/smartIndent.c new/source/smartIndent.c
171 --- old/source/smartIndent.c
172 +++ new/source/smartIndent.c
173 @@ -754,6 +754,7 @@ void BeginSmartIndent(WindowInfo *window
174 "smart indent newline macro", errMsg);
175 return;
177 + GetProgram(winData->newlineMacro);
178 if (indentMacros->modMacro == NULL)
179 winData->modMacro = NULL;
180 else {
181 @@ -766,6 +767,7 @@ void BeginSmartIndent(WindowInfo *window
182 "smart indent modify macro", errMsg);
183 return;
185 + GetProgram(winData->modMacro);
187 window->smartIndentData = (void *)winData;
189 @@ -780,8 +782,8 @@ void EndSmartIndent(WindowInfo *window)
191 /* Free programs and allocated data */
192 if (winData->modMacro != NULL)
193 - FreeProgram(winData->modMacro);
194 - FreeProgram(winData->newlineMacro);
195 + PutProgram(winData->modMacro);
196 + PutProgram(winData->newlineMacro);
197 XtFree((char *)winData);
198 window->smartIndentData = NULL;