3 source/interpret.c | 28 +++++++++++++++++++++-------
4 source/interpret.h | 2 ++
5 source/macro.c | 8 +++++---
7 4 files changed, 30 insertions(+), 11 deletions(-)
9 diff --quilt old/source/interpret.c new/source/interpret.c
10 --- old/source/interpret.c
11 +++ new/source/interpret.c
12 @@ -302,7 +302,7 @@ Program *FinishCreatingProgram(Accumulat
13 newProg->code = (Inst *)XtCalloc(progLen, sizeof(Inst));
14 memcpy(newProg->code, Prog, progLen * sizeof(Inst));
15 newProg->nested = False;
16 - newProg->refcount = 1;
17 + newProg->refcount = 0;
19 DISASM(newProg->name, newProg->code, ProgP - Prog);
21 @@ -314,12 +314,26 @@ Program *FinishCreatingProgram(Accumulat
23 void FreeProgram(Program *prog)
25 + if (prog->refcount > 0) {
26 + fprintf(stderr, "NEdit: warning freeing macro with positive refcount: %s\n",
29 + XtFree((char *)prog->code);
30 + XtFree((char *)prog);
33 +void PutProgram(Program *prog)
35 if (--prog->refcount == 0) {
36 - XtFree((char *)prog->code);
37 - XtFree((char *)prog);
42 +void GetProgram(Program *prog)
48 ** Add an operator (instruction) to the end of the current program
50 @@ -554,7 +568,8 @@ static int setupFrame(RestartData *conte
51 /* cached arg array */
52 *(context->stackP)++ = argArray;
54 - /* prog to free, if requested */
55 + /* frame holds reference to prog */
57 context->stackP->tag = NO_TAG;
58 context->stackP->val.prog = prog;
60 @@ -632,7 +647,7 @@ static void rewindFrame(RestartData *con
62 context->frameP = newFrameP;
69 @@ -685,7 +700,6 @@ int ExecuteMacro(WindowInfo *window, Pro
70 /* pre-set the return PC to NULL */
72 /* prog will be freed by cller, but by stack also, so inc refcount */
74 status = setupFrame(context, prog, nArgs, args, argArray,
75 prog->name ? prog->name : "<exec-macro>");
77 @@ -2718,7 +2732,6 @@ static int callSubroutineFromSymbol(Symb
79 if (sym->value.tag == MACRO_SUBR_TAG) {
80 prog = sym->value.val.prog;
83 return setupNestedFrame(Interpreter, prog);
85 @@ -2941,6 +2954,7 @@ int OverlayRoutineFromProg(Program *prog
89 + /* this is only a helper symbol without a ref to prog */
90 sym.attr = READONLY_ATTR;
91 sym.name = LookupString(prog->name, True);
92 sym.value.tag = MACRO_SUBR_TAG;
93 diff --quilt old/source/interpret.h new/source/interpret.h
94 --- old/source/interpret.h
95 +++ new/source/interpret.h
96 @@ -203,6 +203,8 @@ int AllocNStringCpy(NString *string, con
97 void GarbageCollectStrings(void);
98 void FreeRestartData(RestartData *context);
99 void FreeProgram(Program *prog);
100 +void PutProgram(Program *prog);
101 +void GetProgram(Program *prog);
102 void ModifyReturnedValue(RestartData *context, DataValue dv);
103 WindowInfo *MacroRunWindow(void);
104 WindowInfo *MacroFocusWindow(void);
105 diff --quilt old/source/macro.c new/source/macro.c
106 --- old/source/macro.c
107 +++ new/source/macro.c
108 @@ -1097,6 +1097,7 @@ static void runMacro(WindowInfo *window,
109 window->macroCmdData = cmdData;
110 cmdData->bannerIsUp = False;
111 cmdData->closeOnCompletion = False;
113 cmdData->program = prog;
114 cmdData->context = NULL;
115 cmdData->continueWorkProcID = 0;
116 @@ -1252,7 +1253,7 @@ static void finishMacroCmdExecution(Wind
117 XtDestroyWidget(XtParent(cmdData->dialog));
119 /* Free execution information */
120 - FreeProgram(cmdData->program);
121 + PutProgram(cmdData->program);
122 XtFree((char *)cmdData);
123 window->macroCmdData = NULL;
125 @@ -3925,12 +3926,14 @@ static int defineMS(WindowInfo *window,
126 *errMsg = "Try to override a non macro function.";
129 - FreeProgram(sym->value.val.prog);
131 + PutProgram(sym->value.val.prog);
132 sym->value.val.prog = prog;
136 subrPtr.tag = MACRO_SUBR_TAG;
138 subrPtr.val.prog = prog;
139 sym = InstallSymbol(name, GLOBAL_SCOPE, NO_ATTR, subrPtr);
141 @@ -4263,7 +4266,6 @@ static int evalMS(WindowInfo *window, Da
145 - prog->refcount = 0;
147 ret = OverlayRoutineFromProg(prog, nArgs, nArgs);
149 diff --quilt old/source/parse.y new/source/parse.y
150 --- old/source/parse.y
151 +++ new/source/parse.y
152 @@ -212,7 +212,7 @@ define: definekw blank definesym bla
153 yyerror("try to override built-in subroutine");
156 - FreeProgram(sym->value.val.prog);
157 + PutProgram(sym->value.val.prog);
161 @@ -220,6 +220,7 @@ define: definekw blank definesym bla
162 sym = InstallSymbol(prog->name, GLOBAL_SCOPE, READONLY_ATTR,
166 sym->value.val.prog = prog;