Diff
Not logged in

Differences From Artifact [d25497d984]:

To Artifact [2b8a35635d]:


128
129
130
131
132
133
134
135

136
137

138
139
140
141
142
143
144
128
129
130
131
132
133
134

135
136

137
138
139
140
141
142
143
144







-
+

-
+







static int ckallocInit = 0;

/*
 * Prototypes for procedures defined in this file:
 */

static int		CheckmemCmd(ClientData clientData, Tcl_Interp *interp,
			    int argc, const char *argv[]);
			    int argc, Tcl_Obj *const argv[]);
static int		MemoryCmd(ClientData clientData, Tcl_Interp *interp,
			    int argc, const char *argv[]);
			    int argc, Tcl_Obj *const argv[]);
static void		ValidateMemory(struct mem_header *memHeaderP,
			    const char *file, int line, int nukeGuards);

/*
 *----------------------------------------------------------------------
 *
 * TclInitDbCkalloc --
810
811
812
813
814
815
816
817

818
819
820
821
822

823









824
825
826

827
828
829
830
831
832


833
834
835
836
837
838











839
840
841
842
843
844
845
846

847
848
849
850
851


852
853

854
855

856
857

858
859
860

861
862
863
864
865
866
867
868
869
870


871
872

873
874

875
876

877

878
879
880
881

882
883


884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899


900
901

902
903
904
905


906
907
908
909
910
911
912
913
914


915
916

917
918
919
920
921
922
923

924
925
926

927
928
929


930
931

932
933
934
935
936



937
938
939

940
941

942
943
944
945
946


947
948

949
950
951

952
953

954

955
956
957
958
959
960
961


962
963
964
965
966


967
968
969
970
971
972
973
974
810
811
812
813
814
815
816

817
818
819
820
821

822
823
824
825
826
827
828
829
830
831
832
833
834

835

836
837
838


839
840






841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858

859
860
861
862


863
864
865

866
867

868


869



870
871
872
873
874
875
876
877
878


879
880
881

882
883

884


885

886
887



888
889

890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905


906
907
908

909


910

911
912
913
914
915
916
917
918
919


920
921
922

923

924
925
926
927
928

929
930
931

932
933


934
935
936

937
938




939
940
941

942

943
944

945





946
947
948

949
950


951
952
953
954

955



956
957


958
959

960
961


962
963

964
965
966
967
968
969
970







-
+




-
+

+
+
+
+
+
+
+
+
+


-
+
-



-
-
+
+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+







-
+



-
-
+
+

-
+

-
+
-
-
+
-
-
-
+








-
-
+
+

-
+

-
+
-
-
+
-
+

-
-
-
+

-
+
+














-
-
+
+

-
+
-
-

-
+
+







-
-
+
+

-
+
-





-
+


-
+

-
-
+
+

-
+

-
-
-
-
+
+
+
-

-
+

-
+
-
-
-
-
-
+
+

-
+

-
-
+


+
-
+
-
-
-


-
-
+
+
-


-
-
+
+
-







 */
	/* ARGSUSED */
static int
MemoryCmd(
    ClientData clientData,
    Tcl_Interp *interp,
    int argc,
    const char *argv[])
    Tcl_Obj *const argv[])
{
    const char *fileName;
    FILE *fileP;
    Tcl_DString buffer;
    int result;
    int result, idx;
    size_t len;
    static const char *subcommands[] = {
        "active", "display", "break_on_malloc", "info", "init", "objs",
        "onexit", "tag", "trace", "trace_on_at_malloc", "validate",
        NULL
    };
    enum MemSubcommands {
        OPT_ACTIVE, OPT_DISPLAY, OPT_BREAK, OPT_INFO, OPT_INIT, OPT_OBJS,
        OPT_ONEXIT, OPT_TAG, OPT_TRACE, OPT_TRACEON, OPT_VALIDATE
    };

    if (argc < 2) {
	Tcl_SetObjResult(interp, Tcl_ObjPrintf(
        Tcl_WrongNumArgs(interp, 1, argv, "option [args..]");
                "wrong # args: should be \"%s option [args..]\"", argv[0]));
	return TCL_ERROR;
    }

    if (strcmp(argv[1], "active") == 0 || strcmp(argv[1], "display") == 0) {
	if (argc != 3) {
    if (Tcl_GetIndexFromObj(interp, argv[1], subcommands, "option", 0,
            &idx) != TCL_OK) {
	    Tcl_SetObjResult(interp, Tcl_ObjPrintf(
                    "wrong # args: should be \"%s %s file\"",
                    argv[0], argv[1]));
	    return TCL_ERROR;
	}
	fileName = Tcl_TranslateFileName(interp, argv[2], &buffer);
        return TCL_ERROR;
    }

    switch ((enum MemSubcommands) idx) {
    case OPT_ACTIVE:
    case OPT_DISPLAY:
	if (argc != 3) {
            goto missingFile;
	}
	fileName = Tcl_TranslateFileName(interp, Tcl_GetString(argv[2]),
                &buffer);
	if (fileName == NULL) {
	    return TCL_ERROR;
	}
	result = Tcl_DumpActiveMemory(fileName);
	Tcl_DStringFree(&buffer);
	if (result != TCL_OK) {
	    Tcl_SetObjResult(interp, Tcl_ObjPrintf("error accessing %s: %s",
                    argv[2], Tcl_PosixError(interp)));
                    Tcl_GetString(argv[2]), Tcl_PosixError(interp)));
	    return TCL_ERROR;
	}
	return TCL_OK;
    }
    if (strcmp(argv[1],"break_on_malloc") == 0) {

    case OPT_BREAK:
	if (argc != 3) {
	    goto argError;
	    goto missingCount;
	}
	if (Tcl_GetInt(interp, argv[2], &break_on_malloc) != TCL_OK) {
	return Tcl_GetIntFromObj(interp, argv[2], &break_on_malloc);
	    return TCL_ERROR;
	}

	return TCL_OK;
    }
    if (strcmp(argv[1],"info") == 0) {
    case OPT_INFO:
	Tcl_SetObjResult(interp, Tcl_ObjPrintf(
		"%-25s %10d\n%-25s %10d\n%-25s %10d\n%-25s %10lu\n%-25s %10d\n%-25s %10lu\n",
		"total mallocs", total_mallocs, "total frees", total_frees,
		"current packets allocated", current_malloc_packets,
		"current bytes allocated", (unsigned long)current_bytes_malloced,
		"maximum packets allocated", maximum_malloc_packets,
		"maximum bytes allocated", (unsigned long)maximum_bytes_malloced));
	return TCL_OK;
    }
    if (strcmp(argv[1], "init") == 0) {

    case OPT_INIT:
	if (argc != 3) {
	    goto bad_suboption;
	    goto missingBoolean;
	}
	init_malloced_bodies = (strcmp(argv[2],"on") == 0);
        return Tcl_GetBooleanFromObj(interp, argv[2], &init_malloced_bodies);
	return TCL_OK;
    }

    if (strcmp(argv[1], "objs") == 0) {
    case OPT_OBJS:
	if (argc != 3) {
	    Tcl_SetObjResult(interp, Tcl_ObjPrintf(
                    "wrong # args: should be \"%s objs file\"", argv[0]));
	    return TCL_ERROR;
            goto missingFile;
	}
	fileName = Tcl_TranslateFileName(interp, argv[2], &buffer);
	fileName = Tcl_TranslateFileName(interp, Tcl_GetString(argv[2]),
                &buffer);
	if (fileName == NULL) {
	    return TCL_ERROR;
	}
	fileP = fopen(fileName, "w");
	if (fileP == NULL) {
	    Tcl_SetObjResult(interp, Tcl_ObjPrintf(
                    "cannot open output file: %s",
                    Tcl_PosixError(interp)));
	    return TCL_ERROR;
	}
	TclDbDumpActiveObjects(fileP);
	fclose(fileP);
	Tcl_DStringFree(&buffer);
	return TCL_OK;
    }
    if (strcmp(argv[1],"onexit") == 0) {

    case OPT_ONEXIT:
	if (argc != 3) {
	    Tcl_SetObjResult(interp, Tcl_ObjPrintf(
            goto missingFile;
                    "wrong # args: should be \"%s onexit file\"", argv[0]));
	    return TCL_ERROR;
	}
	fileName = Tcl_TranslateFileName(interp, argv[2], &buffer);
	fileName = Tcl_TranslateFileName(interp, Tcl_GetString(argv[2]),
                &buffer);
	if (fileName == NULL) {
	    return TCL_ERROR;
	}
	onExitMemDumpFileName = dumpFile;
	strcpy(onExitMemDumpFileName,fileName);
	Tcl_DStringFree(&buffer);
	return TCL_OK;
    }
    if (strcmp(argv[1],"tag") == 0) {

    case OPT_TAG:
	if (argc != 3) {
	    Tcl_SetObjResult(interp, Tcl_ObjPrintf(
            Tcl_WrongNumArgs(interp, 2, argv, "string");
                    "wrong # args: should be \"%s tag string\"", argv[0]));
	    return TCL_ERROR;
	}
	if ((curTagPtr != NULL) && (curTagPtr->refCount == 0)) {
	    TclpFree((char *) curTagPtr);
	}
	len = strlen(argv[2]);
	len = strlen(Tcl_GetString(argv[2]));
	curTagPtr = (MemTag *) TclpAlloc(TAG_SIZE(len));
	curTagPtr->refCount = 0;
	memcpy(curTagPtr->string, argv[2], len + 1);
	memcpy(curTagPtr->string, Tcl_GetString(argv[2]), len + 1);
	return TCL_OK;
    }
    if (strcmp(argv[1],"trace") == 0) {

    case OPT_TRACE:
	if (argc != 3) {
	    goto bad_suboption;
	    goto missingBoolean;
	}
	alloc_tracing = (strcmp(argv[2],"on") == 0);
	return TCL_OK;
    }

        return Tcl_GetBooleanFromObj(interp, argv[2], &alloc_tracing);

    case OPT_TRACEON:
    if (strcmp(argv[1],"trace_on_at_malloc") == 0) {
	if (argc != 3) {
	    goto argError;
	    goto missingCount;
	}
	if (Tcl_GetInt(interp, argv[2], &trace_on_at_malloc) != TCL_OK) {
	return Tcl_GetIntFromObj(interp, argv[2], &trace_on_at_malloc);
	    return TCL_ERROR;
	}
	return TCL_OK;
    }
    if (strcmp(argv[1],"validate") == 0) {

    case OPT_VALIDATE:
	if (argc != 3) {
	    goto bad_suboption;
	    goto missingBoolean;
	}
	validate_memory = (strcmp(argv[2],"on") == 0);
	return TCL_OK;
        return Tcl_GetBooleanFromObj(interp, argv[2], &validate_memory);
    }

  missingCount:
    Tcl_SetObjResult(interp, Tcl_ObjPrintf(
    Tcl_WrongNumArgs(interp, 2, argv, "count");
            "bad option \"%s\": should be active, break_on_malloc, info, "
            "init, objs, onexit, tag, trace, trace_on_at_malloc, or validate",
            argv[1]));
    return TCL_ERROR;

  argError:
    Tcl_SetObjResult(interp, Tcl_ObjPrintf(
  missingFile:
    Tcl_WrongNumArgs(interp, 2, argv, "file");
            "wrong # args: should be \"%s %s count\"", argv[0], argv[1]));
    return TCL_ERROR;

  bad_suboption:
    Tcl_SetObjResult(interp, Tcl_ObjPrintf(
  missingBoolean:
    Tcl_WrongNumArgs(interp, 2, argv, "on|off");
            "wrong # args: should be \"%s %s on|off\"", argv[0], argv[1]));
    return TCL_ERROR;
}

/*
 *----------------------------------------------------------------------
 *
 * CheckmemCmd --
987
988
989
990
991
992
993
994

995



996
997
998
999








1000
1001
1002

1003
1004
1005
1006
1007
1008
1009
983
984
985
986
987
988
989

990
991
992
993
994
995



996
997
998
999
1000
1001
1002
1003
1004
1005

1006
1007
1008
1009
1010
1011
1012
1013







-
+

+
+
+

-
-
-
+
+
+
+
+
+
+
+


-
+







 */

static int
CheckmemCmd(
    ClientData clientData,	/* Not used. */
    Tcl_Interp *interp,		/* Interpreter for evaluation. */
    int argc,			/* Number of arguments. */
    const char *argv[])		/* String values of arguments. */
    Tcl_Obj *const argv[])	/* Values of arguments. */
{
    char *bytes;
    int len;

    if (argc != 2) {
	Tcl_SetObjResult(interp, Tcl_ObjPrintf(
                "wrong # args: should be \"%s fileName\"", argv[0]));
	return TCL_ERROR;
        Tcl_WrongNumArgs(interp, 1, argv, "fileName");
	return TCL_ERROR;
    }
    bytes = Tcl_GetStringFromObj(argv[1], &len);
    if (len > 99) {
        Tcl_SetResult(interp, "string too long for internal buffer",
                TCL_STATIC);
        return TCL_ERROR;
    }
    tclMemDumpFileName = dumpFile;
    strcpy(tclMemDumpFileName, argv[1]);
    strcpy(tclMemDumpFileName, bytes);
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_InitMemory --
1021
1022
1023
1024
1025
1026
1027
1028
1029


1030
1031
1032
1033
1034
1035
1036
1025
1026
1027
1028
1029
1030
1031


1032
1033
1034
1035
1036
1037
1038
1039
1040







-
-
+
+








void
Tcl_InitMemory(
    Tcl_Interp *interp)		/* Interpreter in which commands should be
				 * added */
{
    TclInitDbCkalloc();
    Tcl_CreateCommand(interp, "memory", MemoryCmd, NULL, NULL);
    Tcl_CreateCommand(interp, "checkmem", CheckmemCmd, NULL, NULL);
    Tcl_CreateObjCommand(interp, "memory", MemoryCmd, NULL, NULL);
    Tcl_CreateObjCommand(interp, "checkmem", CheckmemCmd, NULL, NULL);
}


#else	/* TCL_MEM_DEBUG */

/* This is the !TCL_MEM_DEBUG case */

1050
1051
1052
1053
1054
1055
1056
1057

1058
1059
1060
1061
1062
1063
1064
1065
1066
1054
1055
1056
1057
1058
1059
1060

1061


1062
1063
1064
1065
1066
1067
1068







-
+
-
-







 *----------------------------------------------------------------------
 */

char *
Tcl_Alloc(
    unsigned int size)
{
    char *result;
    char *result = (char *) TclpAlloc(size);

    result = TclpAlloc(size);

    /*
     * Most systems will not alloc(0), instead bumping it to one so that NULL
     * isn't returned. Some systems (AIX, Tru64) will alloc(0) by returning
     * NULL, so we have to check that the NULL we get is not in response to
     * alloc(0).
     *
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085

1086
1087
1088
1089
1090
1091
1092
1078
1079
1080
1081
1082
1083
1084



1085
1086
1087
1088
1089
1090
1091
1092







-
-
-
+








char *
Tcl_DbCkalloc(
    unsigned int size,
    const char *file,
    int line)
{
    char *result;

    result = (char *) TclpAlloc(size);
    char *result = (char *) TclpAlloc(size);

    if ((result == NULL) && size) {
	fflush(stdout);
	Tcl_Panic("unable to alloc %u bytes, %s line %d", size, file, line);
    }
    return result;
}
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111

1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123

1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143

1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162

1163
1164
1165
1166
1167
1168
1169
1102
1103
1104
1105
1106
1107
1108



1109

1110
1111
1112
1113
1114
1115
1116
1117



1118

1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136

1137


1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151



1152
1153
1154
1155
1156
1157
1158
1159







-
-
-
+
-








-
-
-
+
-


















-
+
-
-














-
-
-
+







 *----------------------------------------------------------------------
 */

char *
Tcl_AttemptAlloc(
    unsigned int size)
{
    char *result;

    result = TclpAlloc(size);
    return (char *) TclpAlloc(size);
    return result;
}

char *
Tcl_AttemptDbCkalloc(
    unsigned int size,
    const char *file,
    int line)
{
    char *result;

    result = (char *) TclpAlloc(size);
    return (char *) TclpAlloc(size);
    return result;
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_Realloc --
 *
 *	Interface to TclpRealloc when TCL_MEM_DEBUG is disabled. It does check
 *	that memory was actually allocated.
 *
 *----------------------------------------------------------------------
 */

char *
Tcl_Realloc(
    char *ptr,
    unsigned int size)
{
    char *result;
    char *result = (char *) TclpRealloc(ptr, size);

    result = TclpRealloc(ptr, size);

    if ((result == NULL) && size) {
	Tcl_Panic("unable to realloc %u bytes", size);
    }
    return result;
}

char *
Tcl_DbCkrealloc(
    char *ptr,
    unsigned int size,
    const char *file,
    int line)
{
    char *result;

    result = (char *) TclpRealloc(ptr, size);
    char *result = (char *) TclpRealloc(ptr, size);

    if ((result == NULL) && size) {
	fflush(stdout);
	Tcl_Panic("unable to realloc %u bytes, %s line %d", size, file, line);
    }
    return result;
}
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189

1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202

1203
1204
1205
1206
1207
1208
1209
1210
1170
1171
1172
1173
1174
1175
1176



1177

1178
1179
1180
1181
1182
1183
1184
1185
1186



1187

1188
1189
1190
1191
1192
1193
1194







-
-
-
+
-









-
-
-
+
-







 */

char *
Tcl_AttemptRealloc(
    char *ptr,
    unsigned int size)
{
    char *result;

    result = TclpRealloc(ptr, size);
    return (char *) TclpRealloc(ptr, size);
    return result;
}

char *
Tcl_AttemptDbCkrealloc(
    char *ptr,
    unsigned int size,
    const char *file,
    int line)
{
    char *result;

    result = (char *) TclpRealloc(ptr, size);
    return (char *) TclpRealloc(ptr, size);
    return result;
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_Free --
 *