remove debug fprintf
[nedit-bw.git] / zero-based-Program-refcount.patch
blobce6bd936e4d8eec71aac3397b78d93a79afe8b00
1 ---
3 source/interpret.c | 28 +++++++++++++++++++++-------
4 source/interpret.h | 2 ++
5 source/macro.c | 8 +++++---
6 source/parse.y | 3 ++-
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",
27 + prog->name);
28 + }
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);
38 + FreeProgram(prog);
42 +void GetProgram(Program *prog)
44 + prog->refcount++;
48 ** Add an operator (instruction) to the end of the current program
50 @@ -557,7 +571,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 */
56 + GetProgram(prog);
57 context->stackP->tag = NO_TAG;
58 context->stackP->val.prog = prog;
59 context->stackP++;
60 @@ -635,7 +650,7 @@ static void rewindFrame(RestartData *con
62 context->frameP = newFrameP;
64 - FreeProgram(prog);
65 + PutProgram(prog);
67 context->pc = newPC;
69 @@ -688,7 +703,6 @@ int ExecuteMacro(WindowInfo *window, Pro
70 /* pre-set the return PC to NULL */
71 context->pc = NULL;
72 /* prog will be freed by cller, but by stack also, so inc refcount */
73 - prog->refcount++;
74 status = setupFrame(context, prog, nArgs, args, argArray,
75 prog->name ? prog->name : "<exec-macro>");
77 @@ -2721,7 +2735,6 @@ static int callSubroutineFromSymbol(Symb
79 if (sym->value.tag == MACRO_SUBR_TAG) {
80 prog = sym->value.val.prog;
81 - prog->refcount++;
82 if (prog->nested) {
83 return setupNestedFrame(Interpreter, prog);
85 @@ -2944,6 +2957,7 @@ int OverlayRoutineFromProg(Program *prog
87 Symbol sym;
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;
112 + GetProgram(prog);
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.";
127 return False;
129 - FreeProgram(sym->value.val.prog);
130 + GetProgram(prog);
131 + PutProgram(sym->value.val.prog);
132 sym->value.val.prog = prog;
134 else {
135 DataValue subrPtr;
136 subrPtr.tag = MACRO_SUBR_TAG;
137 + GetProgram(prog);
138 subrPtr.val.prog = prog;
139 sym = InstallSymbol(name, GLOBAL_SCOPE, NO_ATTR, subrPtr);
141 @@ -4261,7 +4264,6 @@ static int evalMS(WindowInfo *window, Da
142 XtFree(macronl);
144 prog->nested = True;
145 - prog->refcount = 0;
147 ret = OverlayRoutineFromProg(prog, nArgs, nArgs);
148 if (!ret) {
149 diff --quilt old/source/parse.y new/source/parse.y
150 --- old/source/parse.y
151 +++ new/source/parse.y
152 @@ -218,7 +218,7 @@ define: definekw blank definesym bla
153 yyerror("try to override built-in subroutine");
154 YYERROR;
156 - FreeProgram(sym->value.val.prog);
157 + PutProgram(sym->value.val.prog);
159 else {
160 DataValue subrPtr;
161 @@ -226,6 +226,7 @@ define: definekw blank definesym bla
162 sym = InstallSymbol(prog->name, GLOBAL_SCOPE, READONLY_ATTR,
163 subrPtr);
165 + GetProgram(prog);
166 sym->value.val.prog = prog;