Skip to content

Commit 78ad545

Browse files
committed
whpcvt: use global memory functions instead of macros
1 parent 69b428a commit 78ad545

6 files changed

Lines changed: 47 additions & 49 deletions

File tree

bld/whpcvt/c/html.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
*
33
* Open Watcom Project
44
*
5-
* Copyright (c) 2002-2021 The Open Watcom Contributors. All Rights Reserved.
5+
* Copyright (c) 2002-2026 The Open Watcom Contributors. All Rights Reserved.
66
* Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
77
*
88
* ========================================================================
@@ -136,9 +136,9 @@ static char *translate_str_html( const char *str )
136136
}
137137
if( len > Trans_len ) {
138138
if( Trans_str != NULL ) {
139-
_free( Trans_str );
139+
MemFree( Trans_str );
140140
}
141-
_new( Trans_str, len );
141+
Trans_str = MemAllocSafe( len * sizeof( *Trans_str ) );
142142
Trans_len = len;
143143
}
144144
ptr = Trans_str;

bld/whpcvt/c/ipf.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ static char *translate_str_ipf( const char *str )
128128
}
129129
if( len > Trans_len ) {
130130
if( Trans_str != NULL ) {
131-
_free( Trans_str );
131+
MemFree( Trans_str );
132132
}
133-
_new( Trans_str, len );
133+
Trans_str = MemAllocSafe( len * sizeof( *Trans_str ) );
134134
Trans_len = len;
135135
}
136136
ptr = Trans_str;

bld/whpcvt/c/rtf.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@ static char *translate_str_rtf( const char *str, bool do_quotes )
122122
}
123123
if( len > Trans_len ) {
124124
if( Trans_str != NULL ) {
125-
_free( Trans_str );
125+
MemFree( Trans_str );
126126
}
127-
_new( Trans_str, len );
127+
Trans_str = MemAllocSafe( len * sizeof( *Trans_str ) );
128128
Trans_len = len;
129129
}
130130
ptr = Trans_str;

bld/whpcvt/c/whpcvt.c

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ void *MemAlloc( size_t size )
415415
/********************************/
416416
{
417417
#ifdef TRMEM
418-
return( _trmem_alloc( size, _TRMEM_WHO( 1 ), TrHdl ) );
418+
return( _trmem_alloc( size, _TRMEM_WHO( 2 ), TrHdl ) );
419419
#else
420420
return( malloc( size ) );
421421
#endif
@@ -426,7 +426,7 @@ void *MemReallocSafe( void *p, size_t size )
426426
/*******************************************/
427427
{
428428
#ifdef TRMEM
429-
return( check_nomem( _trmem_realloc( p, size, _TRMEM_WHO( 4 ), TrHdl ) ) );
429+
return( check_nomem( _trmem_realloc( p, size, _TRMEM_WHO( 3 ), TrHdl ) ) );
430430
#else
431431
return( check_nomem( realloc( p, size ) ) );
432432
#endif
@@ -443,12 +443,24 @@ void *MemRealloc( void *p, size_t size )
443443
#endif
444444
}
445445

446+
TRMEMAPI( MemStrdupSafe )
447+
char *MemStrdupSafe( const char *str )
448+
{
449+
if( str == NULL )
450+
return( NULL );
451+
#ifdef TRMEM
452+
return( check_nomem( _trmem_strdup( str, _TRMEM_WHO( 5 ), TrHdl ) ) );
453+
#else
454+
return( check_nomem( strdup( str ) ) );
455+
#endif
456+
}
457+
446458
TRMEMAPI( MemFree )
447459
void MemFree( void *ptr )
448460
/************************/
449461
{
450462
#ifdef TRMEM
451-
_trmem_free( ptr, _TRMEM_WHO( 5 ), TrHdl );;
463+
_trmem_free( ptr, _TRMEM_WHO( 6 ), TrHdl );;
452464
#else
453465
free( ptr );
454466
#endif
@@ -527,7 +539,7 @@ void whp_fprintf( FILE *fp, const char *fmt, ... )
527539
va_list args;
528540

529541
if( Chk_buf == NULL ) {
530-
_new( Chk_buf, 10000 );
542+
Chk_buf = MemAllocSafe( 10000 );
531543
}
532544

533545
if( Chk_buf != NULL ) {
@@ -754,8 +766,7 @@ static int process_args( int argc, char *argv[] )
754766
case ARG_TL:
755767
start_arg++;
756768
if( start_arg < argc ) {
757-
_new( Ipf_or_Html_title, strlen( argv[start_arg] ) + 1 );
758-
strcpy( Ipf_or_Html_title, argv[start_arg] );
769+
Ipf_or_Html_title = MemStrdupSafe( argv[start_arg] );
759770
} else {
760771
error_err( ERR_BAD_ARGS );
761772
}
@@ -766,17 +777,15 @@ static int process_args( int argc, char *argv[] )
766777
case ARG_DT:
767778
start_arg++;
768779
if( start_arg < argc ) {
769-
_new( IB_def_topic, strlen( argv[start_arg] ) + 1 );
770-
strcpy( IB_def_topic, argv[start_arg] );
780+
IB_def_topic = MemStrdupSafe( argv[start_arg] );
771781
} else {
772782
error_err( ERR_BAD_ARGS );
773783
}
774784
break;
775785
case ARG_DS:
776786
start_arg++;
777787
if( start_arg < argc ) {
778-
_new( IB_help_desc, strlen( argv[start_arg] ) + 1 );
779-
strcpy( IB_help_desc, argv[start_arg] );
788+
IB_help_desc = MemStrdupSafe( argv[start_arg] );
780789
} else {
781790
error_err( ERR_BAD_ARGS );
782791
}
@@ -919,15 +928,14 @@ static int valid_args( int argc, char *argv[] )
919928
MemFree( Options_File );
920929
Options_File = NULL;
921930

922-
_new( argv, argc );
931+
argv = MemAllocSafe( argc * sizeof( *argv ) );
923932
for( argc = 0;; argc++ ) {
924933
if( fgets( line, sizeof( line ), opt_file ) == NULL ) {
925934
break;
926935
}
927936
line[sizeof( line ) - 1] = '\0';
928937
trim_blanks( line );
929-
_new( argv[argc], strlen( line ) + 1 );
930-
strcpy( argv[argc], line );
938+
argv[argc] = MemStrdupSafe( line );
931939
}
932940
fclose( opt_file );
933941
ret = process_args( argc, argv );
@@ -989,7 +997,7 @@ bool read_line( void )
989997
len++;
990998
if( len > Line_buf_size ) {
991999
Line_buf_size += BUF_GROW;
992-
Line_buf = _realloc( Line_buf, Line_buf_size );
1000+
Line_buf = MemReallocSafe( Line_buf, Line_buf_size );
9931001
buf = &Line_buf[len - 1];
9941002
}
9951003
*buf = (char)c;
@@ -1073,7 +1081,7 @@ size_t trans_add_char( char ch, section_def *section )
10731081
* grow by a good, big amount
10741082
*/
10751083
section->allocated_size += 1024;
1076-
_renew( section->section_text, section->allocated_size );
1084+
section->section_text = MemReallocSafe( section->section_text, section->allocated_size * sizeof( *section->section_text ) );
10771085
}
10781086
section->section_text[section->section_size - 1] = ch;
10791087
return( 1 );
@@ -1106,12 +1114,11 @@ void add_link( const char *link_name )
11061114
{
11071115
link_def *link;
11081116

1109-
_new( link, 1 );
1117+
link = MemAllocSafe( sizeof( *link ) );
11101118

11111119
link->next = Link_list;
11121120
Link_list = link;
1113-
_new( link->link_name, strlen( link_name ) + 1 );
1114-
strcpy( link->link_name, link_name );
1121+
link->link_name = MemStrdupSafe( link_name );
11151122
link->line_num = Line_num;
11161123
}
11171124

@@ -1160,7 +1167,7 @@ static void add_key_ctx( keyword_def *key, ctx_def *ctx )
11601167
/*******************************************************/
11611168
{
11621169
if( key->ctx_list == NULL ) {
1163-
_new( key->ctx_list, 1 );
1170+
key->ctx_list = MemAllocSafe( sizeof( *key->ctx_list ) );
11641171
key->ctx_list_alloc = 1;
11651172
key->ctx_list_size = 0;
11661173
}
@@ -1171,7 +1178,7 @@ static void add_key_ctx( keyword_def *key, ctx_def *ctx )
11711178
* grow by a reasonable amount
11721179
*/
11731180
key->ctx_list_alloc += 16;
1174-
_renew( key->ctx_list, key->ctx_list_alloc );
1181+
key->ctx_list = MemReallocSafe( key->ctx_list, key->ctx_list_alloc * sizeof( *key->ctx_list ) );
11751182
}
11761183
key->ctx_list[key->ctx_list_size - 1] = ctx;
11771184
}
@@ -1188,11 +1195,10 @@ void add_ctx_keyword( ctx_def *ctx, const char *keyword )
11881195
if( key != NULL ) {
11891196
key->duplicate = true;
11901197
} else {
1191-
_new( key, 1 );
1198+
key = MemAllocSafe( sizeof( *key ) );
11921199
key->duplicate = false;
11931200
key->defined_ctx = ctx;
1194-
_new( key->keyword, strlen( keyword ) + 1 );
1195-
strcpy( key->keyword, keyword );
1201+
key->keyword = MemStrdupSafe( keyword );
11961202
key->next = Keyword_list;
11971203
key->id = Keyword_id;
11981204
Keyword_id++;
@@ -1202,7 +1208,7 @@ void add_ctx_keyword( ctx_def *ctx, const char *keyword )
12021208
}
12031209
add_key_ctx( key, ctx );
12041210

1205-
_new( keylist, 1 );
1211+
keylist = MemAllocSafe( sizeof( *keylist ) );
12061212
keylist->key = key;
12071213
keylist->next = ctx->keylist;
12081214
ctx->keylist = keylist;
@@ -1274,7 +1280,7 @@ static bool read_topic_text( ctx_def *ctx, bool is_blank, int order_num )
12741280
break;
12751281
}
12761282
if( section == NULL ) {
1277-
_new( section, 1 );
1283+
section = MemAllocSafe( sizeof( *section ) );
12781284
section->section_text = NULL;
12791285
section->allocated_size = 0;
12801286
section->section_size = 0;
@@ -1392,9 +1398,8 @@ static browse_def *add_browse( const char *browse_name, ctx_def *ctx )
13921398
browse_prev = NULL;
13931399
for( browse = Browse_list;; browse_prev = browse, browse = browse->next ) {
13941400
if( browse == NULL ) {
1395-
_new( browse, 1 );
1396-
_new( browse->browse_name, strlen( browse_name ) + 1 );
1397-
strcpy( browse->browse_name, browse_name );
1401+
browse = MemAllocSafe( sizeof( *browse ) );
1402+
browse->browse_name = MemStrdupSafe( browse_name );
13981403
browse->ctx_list = NULL;
13991404
browse->next = NULL;
14001405
/*
@@ -1411,7 +1416,7 @@ static browse_def *add_browse( const char *browse_name, ctx_def *ctx )
14111416
}
14121417
}
14131418

1414-
_new( b_ctx, 1 );
1419+
b_ctx = MemAllocSafe( sizeof( *b_ctx ) );
14151420
b_ctx->ctx = ctx;
14161421

14171422
for( b_ctx_list = &browse->ctx_list; *b_ctx_list != NULL; b_ctx_list = &((*b_ctx_list)->next) ) {
@@ -1437,8 +1442,7 @@ static void add_ctx( ctx_def *ctx, const char *title, char *keywords, const char
14371442
ctx_def *ctx_list;
14381443

14391444
if( title != NULL && ctx->title == NULL ) {
1440-
_new( ctx->title, strlen( title ) + 1 );
1441-
strcpy( ctx->title, title );
1445+
ctx->title = MemStrdupSafe( title );
14421446
}
14431447
if( keywords != NULL && ctx->keylist == NULL && *skip_blanks( keywords ) != '\0' ) {
14441448
for( ptr = keywords;; ) {
@@ -1475,7 +1479,7 @@ static ctx_def *init_ctx( char *ctx_name )
14751479
{
14761480
ctx_def *ctx;
14771481

1478-
_new( ctx, 1 );
1482+
ctx = MemAllocSafe( sizeof( *ctx ) );
14791483
ctx->section_list = NULL;
14801484
if( Ctx_list == NULL ) {
14811485
Ctx_list = ctx;
@@ -1490,8 +1494,7 @@ static ctx_def *init_ctx( char *ctx_name )
14901494
ctx->ctx_id = -1;
14911495
ctx->empty = true;
14921496
ctx->req_by_link = false;
1493-
_new( ctx->ctx_name, strlen( ctx_name ) + 1 );
1494-
strcpy( ctx->ctx_name, ctx_name );
1497+
ctx->ctx_name = MemStrdupSafe( ctx_name );
14951498

14961499
return( ctx );
14971500
}
@@ -1847,7 +1850,7 @@ static void output_kw_file( void )
18471850
/*
18481851
* ... we allocate an array of pointers to keywords ...
18491852
*/
1850-
_new( kw, kw_num );
1853+
kw = MemAllocSafe( kw_num * sizeof( *kw ) );
18511854
/*
18521855
* ... fill it up ...
18531856
*/

bld/whpcvt/c/wiki.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ static char *translate_str_wiki( const char *str )
133133
}
134134
if( len > Trans_len ) {
135135
if( Trans_str != NULL ) {
136-
_free( Trans_str );
136+
MemFree( Trans_str );
137137
}
138-
_new( Trans_str, len );
138+
Trans_str = MemAllocSafe( len * sizeof( *Trans_str ) );
139139
Trans_len = len;
140140
}
141141
ptr = Trans_str;

bld/whpcvt/h/whpcvt.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,6 @@
5555

5656
#define _min( a, b ) ( a > b ) b ? a
5757
#define _max( a, b ) ( a > b ) a ? b
58-
#define _new( ptr, size ) ( ptr = MemAllocSafe( sizeof( *(ptr) ) * (size) ) )
59-
#define _renew( ptr, size ) ( ptr = MemReallocSafe( ptr, sizeof( *(ptr) ) * (size) ) )
60-
#define _free( x ) MemFree( x )
61-
#define _alloc( size ) MemAllocSafe( size )
62-
#define _realloc( ptr, size ) MemReallocSafe( ptr, size )
6358

6459
#define _is_nonblank( c ) ((c) != '\0' && (c) != ' ' && (c) != '\t')
6560

0 commit comments

Comments
 (0)