+allowed a user to define the memory tag data type, unsigned short as default; +added an mb_set_memory_manager function, allows user defined memory management; +added referencable data type size constants.

This commit is contained in:
tony 2015-08-25 21:42:15 +08:00
parent 802b5aa1f1
commit 78fe066a29
14 changed files with 83 additions and 30 deletions

View File

@ -1,3 +1,8 @@
Aug. 25 2015
Allowed a user to define the memory tag data type, unsigned short as default
Added an mb_set_memory_manager function, allows user defined memory management
Added referencable data type size constants
Aug. 11 2015 Aug. 11 2015
Fixed a wrong IF trunk processing bug, thanks to irony for pointing it out Fixed a wrong IF trunk processing bug, thanks to irony for pointing it out
Fixed a crash bug with an invalid IF statement Fixed a crash bug with an invalid IF statement
@ -73,8 +78,8 @@ Dec. 9 2014
Improved compatibility with BCB Improved compatibility with BCB
May. 25 2014 May. 25 2014
Added an mb_set_inputer function which allows the user to specify an INPUT reader, thanks to Michael P. Welch for suggestion Added an mb_set_inputer function which allows a user to specify an INPUT reader, thanks to Michael P. Welch for suggestion
Added an mb_remove_reserved_func function which allows the user to disable/remove a reserved statement Added an mb_remove_reserved_func function which allows a user to disable/remove a reserved statement
May. 22 2014 May. 22 2014
Fixed a crash bug when missing colon in a combined line, thanks to Michael P. Welch for pointing it out Fixed a crash bug when missing colon in a combined line, thanks to Michael P. Welch for pointing it out

2
LICENSE Normal file → Executable file
View File

@ -1,6 +1,6 @@
The MIT License The MIT License
Copyright (c) 2011 - 2015 W. Renxin Copyright (c) 2011 - 2015 Wang Renxin
Permission is hereby granted, free of charge, to any person obtaining a copy of Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in this software and associated documentation files (the "Software"), to deal in

View File

@ -57,6 +57,7 @@ For more details about using MY-BASIC when it's already integrated with exist pr
* [Passes](https://github.com/paladin-t/my_basic/wiki/Passes) * [Passes](https://github.com/paladin-t/my_basic/wiki/Passes)
* [Interpreter workflow diagram](https://github.com/paladin-t/my_basic/wiki/Interpreter-workflow-diagram) * [Interpreter workflow diagram](https://github.com/paladin-t/my_basic/wiki/Interpreter-workflow-diagram)
* [Link with MY-BASIC](https://github.com/paladin-t/my_basic/wiki/Link-with-MY_BASIC) * [Link with MY-BASIC](https://github.com/paladin-t/my_basic/wiki/Link-with-MY_BASIC)
* [Customize a memory allocator](https://github.com/paladin-t/my_basic/wiki/Customize-a-memory-allocator)
* [Redirect PRINT and INPUT](https://github.com/paladin-t/my_basic/wiki/Redirect-PRINT-and-INPUT) * [Redirect PRINT and INPUT](https://github.com/paladin-t/my_basic/wiki/Redirect-PRINT-and-INPUT)
* [Redefine int_t and real_t](https://github.com/paladin-t/my_basic/wiki/Redefine-int_t-and-real_t) * [Redefine int_t and real_t](https://github.com/paladin-t/my_basic/wiki/Redefine-int_t-and-real_t)
* [Use usertype values](https://github.com/paladin-t/my_basic/wiki/Use-usertype-values) * [Use usertype values](https://github.com/paladin-t/my_basic/wiki/Use-usertype-values)

View File

@ -3,7 +3,7 @@
** **
** For the latest info, see https://github.com/paladin-t/my_basic/ ** For the latest info, see https://github.com/paladin-t/my_basic/
** **
** Copyright (C) 2011 - 2015 W. Renxin ** Copyright (C) 2011 - 2015 Wang Renxin
** **
** Permission is hereby granted, free of charge, to any person obtaining a copy of ** Permission is hereby granted, free of charge, to any person obtaining a copy of
** this software and associated documentation files (the "Software"), to deal in ** this software and associated documentation files (the "Software"), to deal in
@ -78,7 +78,7 @@ extern "C" {
/** Macros */ /** Macros */
#define _VER_MAJOR 1 #define _VER_MAJOR 1
#define _VER_MINOR 1 #define _VER_MINOR 1
#define _VER_REVISION 62 #define _VER_REVISION 63
#define _MB_VERSION ((_VER_MAJOR * 0x01000000) + (_VER_MINOR * 0x00010000) + (_VER_REVISION)) #define _MB_VERSION ((_VER_MAJOR * 0x01000000) + (_VER_MINOR * 0x00010000) + (_VER_REVISION))
/* Uncomment this line to treat warnings as error */ /* Uncomment this line to treat warnings as error */
@ -299,6 +299,31 @@ typedef struct _object_t {
#endif /* MB_ENABLE_SOURCE_TRACE */ #endif /* MB_ENABLE_SOURCE_TRACE */
} _object_t; } _object_t;
typedef unsigned short _mem_tag_t;
#define _MB_MEM_TAG_SIZE (sizeof(_mem_tag_t))
#ifdef MB_ENABLE_ALLOC_STAT
const size_t MB_SIZEOF_INT = _MB_MEM_TAG_SIZE + sizeof(int);
const size_t MB_SIZEOF_PTR = _MB_MEM_TAG_SIZE + sizeof(intptr_t);
const size_t MB_SIZEOF_LSN = _MB_MEM_TAG_SIZE + sizeof(_ls_node_t);
const size_t MB_SIZEOF_HTN = _MB_MEM_TAG_SIZE + sizeof(_ht_node_t);
const size_t MB_SIZEOF_OBJ = _MB_MEM_TAG_SIZE + sizeof(_object_t);
const size_t MB_SIZEOF_FUN = _MB_MEM_TAG_SIZE + sizeof(_func_t);
const size_t MB_SIZEOF_ARR = _MB_MEM_TAG_SIZE + sizeof(_array_t);
const size_t MB_SIZEOF_VAR = _MB_MEM_TAG_SIZE + sizeof(_var_t);
const size_t MB_SIZEOF_LBL = _MB_MEM_TAG_SIZE + sizeof(_label_t);
#else /* MB_ENABLE_ALLOC_STAT */
const size_t MB_SIZEOF_INT = sizeof(int);
const size_t MB_SIZEOF_PTR = sizeof(intptr_t);
const size_t MB_SIZEOF_LSN = sizeof(_ls_node_t);
const size_t MB_SIZEOF_HTN = sizeof(_ht_node_t);
const size_t MB_SIZEOF_OBJ = sizeof(_object_t);
const size_t MB_SIZEOF_FUN = sizeof(_func_t);
const size_t MB_SIZEOF_ARR = sizeof(_array_t);
const size_t MB_SIZEOF_VAR = sizeof(_var_t);
const size_t MB_SIZEOF_LBL = sizeof(_label_t);
#endif /* MB_ENABLE_ALLOC_STAT */
#ifdef MB_ENABLE_SOURCE_TRACE #ifdef MB_ENABLE_SOURCE_TRACE
static const _object_t _OBJ_INT_UNIT = { _DT_INT, 1, false, 0, 0, 0 }; static const _object_t _OBJ_INT_UNIT = { _DT_INT, 1, false, 0, 0, 0 };
static const _object_t _OBJ_INT_ZERO = { _DT_INT, 0, false, 0, 0, 0 }; static const _object_t _OBJ_INT_ZERO = { _DT_INT, 0, false, 0, 0, 0 };
@ -606,9 +631,9 @@ static void _ht_clear(_ht_node_t* ht);
static void _ht_destroy(_ht_node_t* ht); static void _ht_destroy(_ht_node_t* ht);
/** Memory operations */ /** Memory operations */
#define _MB_POINTER_SIZE (sizeof(intptr_t)) #define _MB_CHECK_MEM_TAG_SIZE(y, s) do { _mem_tag_t _tmp = (_mem_tag_t)s; if((y)_tmp != s) { mb_assert(0 && "\"_mem_tag_t\" is too small."); printf("The type \"_mem_tag_t\" is not precise enough to hold the given data, please redefine it!"); } } while(0)
#define _MB_WRITE_CHUNK_SIZE(t, s) (*((size_t*)((char*)(t) - _MB_POINTER_SIZE)) = s) #define _MB_WRITE_MEM_TAG_SIZE(t, s) (*((_mem_tag_t*)((char*)(t) - _MB_MEM_TAG_SIZE)) = (_mem_tag_t)s)
#define _MB_READ_CHUNK_SIZE(t) (*((size_t*)((char*)(t) - _MB_POINTER_SIZE))) #define _MB_READ_MEM_TAG_SIZE(t) (*((_mem_tag_t*)((char*)(t) - _MB_MEM_TAG_SIZE)))
#ifdef MB_ENABLE_ALLOC_STAT #ifdef MB_ENABLE_ALLOC_STAT
static volatile size_t _mb_allocated = 0; static volatile size_t _mb_allocated = 0;
@ -616,6 +641,9 @@ static volatile size_t _mb_allocated = 0;
static volatile size_t _mb_allocated = (size_t)(~0); static volatile size_t _mb_allocated = (size_t)(~0);
#endif /* MB_ENABLE_ALLOC_STAT */ #endif /* MB_ENABLE_ALLOC_STAT */
static mb_memory_allocate_func_t _mb_allocate_func = 0;
static mb_memory_free_func_t _mb_free_func = 0;
static void* mb_malloc(size_t s); static void* mb_malloc(size_t s);
static void mb_free(void* p); static void mb_free(void* p);
@ -1305,14 +1333,18 @@ void* mb_malloc(size_t s) {
char* ret = NULL; char* ret = NULL;
size_t rs = s; size_t rs = s;
#ifdef MB_ENABLE_ALLOC_STAT #ifdef MB_ENABLE_ALLOC_STAT
rs += _MB_POINTER_SIZE; _MB_CHECK_MEM_TAG_SIZE(size_t, s);
rs += _MB_MEM_TAG_SIZE;
#endif /* MB_ENABLE_ALLOC_STAT */ #endif /* MB_ENABLE_ALLOC_STAT */
if(_mb_allocate_func)
ret = _mb_allocate_func((unsigned)rs);
else
ret = (char*)malloc(rs); ret = (char*)malloc(rs);
mb_assert(ret); mb_assert(ret);
#ifdef MB_ENABLE_ALLOC_STAT #ifdef MB_ENABLE_ALLOC_STAT
_mb_allocated += s; _mb_allocated += s;
ret += _MB_POINTER_SIZE; ret += _MB_MEM_TAG_SIZE;
_MB_WRITE_CHUNK_SIZE(ret, s); _MB_WRITE_MEM_TAG_SIZE(ret, s);
#endif /* MB_ENABLE_ALLOC_STAT */ #endif /* MB_ENABLE_ALLOC_STAT */
return (void*)ret; return (void*)ret;
@ -1323,12 +1355,15 @@ void mb_free(void* p) {
#ifdef MB_ENABLE_ALLOC_STAT #ifdef MB_ENABLE_ALLOC_STAT
do { do {
size_t os = _MB_READ_CHUNK_SIZE(p); size_t os = _MB_READ_MEM_TAG_SIZE(p);
_mb_allocated -= os; _mb_allocated -= os;
p = (char*)p - _MB_POINTER_SIZE; p = (char*)p - _MB_MEM_TAG_SIZE;
} while(0); } while(0);
#endif /* MB_ENABLE_ALLOC_STAT */ #endif /* MB_ENABLE_ALLOC_STAT */
if(_mb_free_func)
_mb_free_func(p);
else
free(p); free(p);
} }
@ -4443,6 +4478,15 @@ char* mb_memdup(char* val, unsigned size) {
return result; return result;
} }
int mb_set_memory_manager(mb_memory_allocate_func_t a, mb_memory_free_func_t f) {
/* Register an allocator and a freer globally */
_mb_allocate_func = a;
_mb_free_func = f;
return 0;
}
/* ========================================================} */ /* ========================================================} */
/* /*

View File

@ -3,7 +3,7 @@
** **
** For the latest info, see https://github.com/paladin-t/my_basic/ ** For the latest info, see https://github.com/paladin-t/my_basic/
** **
** Copyright (C) 2011 - 2015 W. Renxin ** Copyright (C) 2011 - 2015 Wang Renxin
** **
** Permission is hereby granted, free of charge, to any person obtaining a copy of ** Permission is hereby granted, free of charge, to any person obtaining a copy of
** this software and associated documentation files (the "Software"), to deal in ** this software and associated documentation files (the "Software"), to deal in
@ -248,6 +248,8 @@ typedef void (* mb_debug_stepped_handler_t)(struct mb_interpreter_t*, int, unsig
typedef void (* mb_error_handler_t)(struct mb_interpreter_t*, enum mb_error_e, char*, int, unsigned short, unsigned short, int); typedef void (* mb_error_handler_t)(struct mb_interpreter_t*, enum mb_error_e, char*, int, unsigned short, unsigned short, int);
typedef int (* mb_print_func_t)(const char*, ...); typedef int (* mb_print_func_t)(const char*, ...);
typedef int (* mb_input_func_t)(char*, int); typedef int (* mb_input_func_t)(char*, int);
typedef char* (* mb_memory_allocate_func_t)(unsigned s);
typedef void (* mb_memory_free_func_t)(char* p);
MBAPI unsigned int mb_ver(void); MBAPI unsigned int mb_ver(void);
MBAPI const char* mb_ver_string(void); MBAPI const char* mb_ver_string(void);
@ -302,6 +304,7 @@ MBAPI int mb_set_inputer(struct mb_interpreter_t* s, mb_input_func_t p);
MBAPI int mb_gets(char* buf, int s); MBAPI int mb_gets(char* buf, int s);
MBAPI char* mb_memdup(char* val, unsigned size); MBAPI char* mb_memdup(char* val, unsigned size);
MBAPI int mb_set_memory_manager(mb_memory_allocate_func_t a, mb_memory_free_func_t f);
#ifdef MB_COMPACT_MODE #ifdef MB_COMPACT_MODE
# pragma pack() # pragma pack()

Binary file not shown.

Binary file not shown.

View File

@ -36,8 +36,8 @@
IDI_ICON_MAIN ICON "icon.ico" IDI_ICON_MAIN ICON "icon.ico"
VS_VERSION_INFO VERSIONINFO VS_VERSION_INFO VERSIONINFO
FILEVERSION 1,1,61,0 FILEVERSION 1,1,63,0
PRODUCTVERSION 1,1,61,0 PRODUCTVERSION 1,1,63,0
FILEFLAGSMASK 0x17L FILEFLAGSMASK 0x17L
# ifdef _DEBUG # ifdef _DEBUG
FILEFLAGS 0x1L FILEFLAGS 0x1L
@ -53,15 +53,15 @@
BLOCK "080404b0" BLOCK "080404b0"
BEGIN BEGIN
VALUE "Comments", "MY-BASIC" VALUE "Comments", "MY-BASIC"
VALUE "CompanyName", "W. Renxin" VALUE "CompanyName", "Wang Renxin"
VALUE "FileDescription", "MY-BASIC interpreter" VALUE "FileDescription", "MY-BASIC Interpreter for Windows"
VALUE "FileVersion", "1, 1, 61, 0" VALUE "FileVersion", "1, 1, 63, 0"
VALUE "InternalName", "my_basic" VALUE "InternalName", "my_basic"
VALUE "LegalCopyright", "Copyright (C) 2011 - 2015 W. Renxin" VALUE "LegalCopyright", "Copyright (C) 2011 - 2015 Wang Renxin"
VALUE "LegalTrademarks", "MY-BASIC" VALUE "LegalTrademarks", "MY-BASIC"
VALUE "OriginalFilename", "my_basic.exe" VALUE "OriginalFilename", "my_basic.exe"
VALUE "ProductName", "MY-BASIC" VALUE "ProductName", "MY-BASIC"
VALUE "ProductVersion", "1, 1, 61, 0" VALUE "ProductVersion", "1, 1, 63, 0"
END END
END END
BLOCK "VarFileInfo" BLOCK "VarFileInfo"

View File

@ -1,5 +1,5 @@
' This script is an example of MY-BASIC ' This script is an example of MY-BASIC
' Copyright (c) 2011 - 2015 W. Renxin. All rights reserved. ' Copyright (c) 2011 - 2015 Wang Renxin. All rights reserved.
' For more information, see https://github.com/paladin-t/my_basic/ ' For more information, see https://github.com/paladin-t/my_basic/
s$ = "hello " s$ = "hello "

View File

@ -1,5 +1,5 @@
' This script is an example of MY-BASIC ' This script is an example of MY-BASIC
' Copyright (c) 2011 - 2015 W. Renxin. All rights reserved. ' Copyright (c) 2011 - 2015 Wang Renxin. All rights reserved.
' For more information, see https://github.com/paladin-t/my_basic/ ' For more information, see https://github.com/paladin-t/my_basic/
e = 50 e = 50

View File

@ -1,5 +1,5 @@
' This script is an example of MY-BASIC ' This script is an example of MY-BASIC
' Copyright (c) 2011 - 2015 W. Renxin. All rights reserved. ' Copyright (c) 2011 - 2015 Wang Renxin. All rights reserved.
' For more information, see https://github.com/paladin-t/my_basic/ ' For more information, see https://github.com/paladin-t/my_basic/
PRINT "Input: " PRINT "Input: "

View File

@ -1,5 +1,5 @@
' This script is an example of MY-BASIC ' This script is an example of MY-BASIC
' Copyright (c) 2011 - 2015 W. Renxin. All rights reserved. ' Copyright (c) 2011 - 2015 Wang Renxin. All rights reserved.
' For more information, see https://github.com/paladin-t/my_basic/ ' For more information, see https://github.com/paladin-t/my_basic/
BEGIN: BEGIN:

View File

@ -3,7 +3,7 @@
** **
** For the latest info, see https://github.com/paladin-t/my_basic/ ** For the latest info, see https://github.com/paladin-t/my_basic/
** **
** Copyright (C) 2011 - 2015 W. Renxin ** Copyright (C) 2011 - 2015 Wang Renxin
** **
** Permission is hereby granted, free of charge, to any person obtaining a copy of ** Permission is hereby granted, free of charge, to any person obtaining a copy of
** this software and associated documentation files (the "Software"), to deal in ** this software and associated documentation files (the "Software"), to deal in
@ -394,7 +394,7 @@ static void _kill_program(const char* path) {
static void _show_tip(void) { static void _show_tip(void) {
printf("MY-BASIC Interpreter Shell - %s.\n", mb_ver_string()); printf("MY-BASIC Interpreter Shell - %s.\n", mb_ver_string());
printf("Copyright (C) 2011 - 2015 W. Renxin. All Rights Reserved.\n"); printf("Copyright (C) 2011 - 2015 Wang Renxin. All Rights Reserved.\n");
printf("For more information, see https://github.com/paladin-t/my_basic/.\n"); printf("For more information, see https://github.com/paladin-t/my_basic/.\n");
printf("Input HELP and hint enter to view help information.\n"); printf("Input HELP and hint enter to view help information.\n");
} }