mirror of
https://github.com/protobuf-c/protobuf-c.git
synced 2024-12-28 14:48:18 +08:00
protobuf-c: remove alloc_failed_warning and protobuf_c_out_of_memory
in general, libraries shouldn't be responsible for terminating the program if memory allocation fails. if we need to allocate memory and can't, we should be returning a failure indicator, not providing a strange interface to the user for receiving a callback in the event of such an error. also in general, libraries should never write to stdout or stderr. this breaks the API/ABI and will require a note in the ChangeLog.
This commit is contained in:
parent
8cd5f6764b
commit
8216865a46
@ -81,7 +81,6 @@ do { \
|
|||||||
} else if ((dst=((allocator)->alloc((allocator)->allocator_data,\
|
} else if ((dst=((allocator)->alloc((allocator)->allocator_data,\
|
||||||
da__allocation_size))) == NULL) \
|
da__allocation_size))) == NULL) \
|
||||||
{ \
|
{ \
|
||||||
alloc_failed_warning(da__allocation_size, __FILE__, __LINE__);\
|
|
||||||
fail_code; \
|
fail_code; \
|
||||||
} \
|
} \
|
||||||
} while (0)
|
} while (0)
|
||||||
@ -118,25 +117,8 @@ do { \
|
|||||||
unsigned protobuf_c_major = PROTOBUF_C_MAJOR;
|
unsigned protobuf_c_major = PROTOBUF_C_MAJOR;
|
||||||
unsigned protobuf_c_minor = PROTOBUF_C_MINOR;
|
unsigned protobuf_c_minor = PROTOBUF_C_MINOR;
|
||||||
|
|
||||||
static void
|
|
||||||
alloc_failed_warning(unsigned size, const char *filename, unsigned line)
|
|
||||||
{
|
|
||||||
fprintf(stderr,
|
|
||||||
"WARNING: out-of-memory allocating a block of size %u (%s:%u)\n",
|
|
||||||
size, filename, line);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* --- allocator --- */
|
/* --- allocator --- */
|
||||||
|
|
||||||
static void
|
|
||||||
protobuf_c_out_of_memory_default(void)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Out Of Memory!!!\n");
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
|
|
||||||
void (*protobuf_c_out_of_memory)(void) = protobuf_c_out_of_memory_default;
|
|
||||||
|
|
||||||
static void *
|
static void *
|
||||||
system_alloc(void *allocator_data, size_t size)
|
system_alloc(void *allocator_data, size_t size)
|
||||||
{
|
{
|
||||||
@ -146,8 +128,6 @@ system_alloc(void *allocator_data, size_t size)
|
|||||||
if (size == 0)
|
if (size == 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
rv = malloc(size);
|
rv = malloc(size);
|
||||||
if (rv == NULL)
|
|
||||||
protobuf_c_out_of_memory();
|
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -158,13 +158,6 @@ struct _ProtobufCAllocator
|
|||||||
*/
|
*/
|
||||||
extern PROTOBUF_C_API ProtobufCAllocator protobuf_c_default_allocator; /* settable */
|
extern PROTOBUF_C_API ProtobufCAllocator protobuf_c_default_allocator; /* settable */
|
||||||
|
|
||||||
/*
|
|
||||||
* This is the function that our default allocators call when they run
|
|
||||||
* out-of-memory. The default behavior of this function is to terminate your
|
|
||||||
* program.
|
|
||||||
*/
|
|
||||||
extern PROTOBUF_C_API void (*protobuf_c_out_of_memory)(void);
|
|
||||||
|
|
||||||
/* --- append-only data buffer --- */
|
/* --- append-only data buffer --- */
|
||||||
typedef struct _ProtobufCBuffer ProtobufCBuffer;
|
typedef struct _ProtobufCBuffer ProtobufCBuffer;
|
||||||
struct _ProtobufCBuffer {
|
struct _ProtobufCBuffer {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user