protobuf-c/: gratuitous style changes

dave's original style drives me crazy. reformat the C code in
protobuf-c/ with "indent -kr -i8" and manually reflow for readability.

try to fit most lines in 80 columns, but due to the lengthy type and
function names in protobuf-c, enforcing an 80 column rule would result
in a lot of cramped statements, so try to fit lines in up to 100 columns
if it would improve readability. (e.g., one <=100 column line is
probably better than 3-4 <=80 column lines.)

ultimately i'd like to adopt most of the recommendations in the linux
coding style: https://www.kernel.org/doc/Documentation/CodingStyle.
this commit gets us most of the kernel indentation and comment coding
style recommendations. later commits will tackle style recommendations
that require more intrusive changes: breaking up large functions,
replacing macros that affect control flow (e.g., DO_ALLOC). this will
hopefully facilitate review and make the code base easier to maintain.

i ran the old and new versions of protobuf-c.c through something like:

  gcc -S -D__PRETTY_FUNCTION__=0 -D__FILE__=0 -D__LINE__=0 -Wall -O0 \
    -o protobuf-c.S -c protobuf-c.c

and reviewed the diffs of the assembly output to spot any functions that
changed, and went back to make sure that any differences were
functionally equivalent.
This commit is contained in:
Robert Edmonds 2014-01-11 13:41:11 -05:00
parent 934ff223db
commit c797b07eec
3 changed files with 2901 additions and 2804 deletions

View File

@ -28,11 +28,16 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
/* A little enum helper macro: this will ensure that your
enum's size is sizeof(int). In protobuf, it need not
be larger than 32-bits.
This is written assuming it is appended to a list w/o a tail comma. */ #define PROTOBUF_C_SERVICE_DESCRIPTOR_MAGIC 0x14159bc3
#define PROTOBUF_C_MESSAGE_DESCRIPTOR_MAGIC 0x28aaeef9
#define PROTOBUF_C_ENUM_DESCRIPTOR_MAGIC 0x114315af
/*
* A little enum helper macro: this will ensure that your enum's size is
* sizeof(int). In protobuf, it need not be larger than 32-bits. This is
* written assuming it is appended to a list w/o a tail comma.
*/
#ifndef _PROTOBUF_C_FORCE_ENUM_TO_BE_INT_SIZE #ifndef _PROTOBUF_C_FORCE_ENUM_TO_BE_INT_SIZE
#define _PROTOBUF_C_FORCE_ENUM_TO_BE_INT_SIZE(enum_name) \ #define _PROTOBUF_C_FORCE_ENUM_TO_BE_INT_SIZE(enum_name) \
, _##enum_name##_IS_INT_SIZE = INT_MAX , _##enum_name##_IS_INT_SIZE = INT_MAX
@ -40,55 +45,60 @@
/* === needs to be declared for the PROTOBUF_C_BUFFER_SIMPLE_INIT macro === */ /* === needs to be declared for the PROTOBUF_C_BUFFER_SIMPLE_INIT macro === */
void protobuf_c_buffer_simple_append (ProtobufCBuffer *buffer, void
protobuf_c_buffer_simple_append(
ProtobufCBuffer *buffer,
size_t len, size_t len,
const unsigned char *data); const unsigned char *data);
/* === stuff which needs to be declared for use in the generated code === */ /* === stuff which needs to be declared for use in the generated code === */
struct _ProtobufCEnumValueIndex struct _ProtobufCEnumValueIndex {
{
const char *name; const char *name;
unsigned index; /* into values[] array */ unsigned index; /* into values[] array */
}; };
/* IntRange: helper structure for optimizing /*
int => index lookups * IntRange: helper structure for optimizing int => index lookups in the case
in the case where the keys are mostly consecutive values, * where the keys are mostly consecutive values, as they presumably are for
as they presumably are for enums and fields. * enums and fields.
*
The data structures assumes that the values in the original * The data structures assumes that the values in the original array are
array are sorted */ * sorted.
struct _ProtobufCIntRange */
{ struct _ProtobufCIntRange {
int start_value; int start_value;
unsigned orig_index; unsigned orig_index;
/* NOTE: the number of values in the range can /*
be inferred by looking at the next element's orig_index. * NOTE: the number of values in the range can be inferred by looking
a dummy element is added to make this simple */ * at the next element's orig_index. A dummy element is added to make
* this simple.
*/
}; };
/* === declared for exposition on ProtobufCIntRange === */ /* === declared for exposition on ProtobufCIntRange === */
/* note: ranges must have an extra sentinel IntRange at the end whose
orig_index is set to the number of actual values in the original array */
/* returns -1 if no orig_index found */
int protobuf_c_int_ranges_lookup (unsigned n_ranges,
ProtobufCIntRange *ranges);
#define PROTOBUF_C_SERVICE_DESCRIPTOR_MAGIC 0x14159bc3 /*
#define PROTOBUF_C_MESSAGE_DESCRIPTOR_MAGIC 0x28aaeef9 * Note: ranges must have an extra sentinel IntRange at the end whose
#define PROTOBUF_C_ENUM_DESCRIPTOR_MAGIC 0x114315af * orig_index is set to the number of actual values in the original array.
* Returns -1 if no orig_index found.
*/
int
protobuf_c_int_ranges_lookup(unsigned n_ranges, ProtobufCIntRange *ranges);
/* === behind the scenes on the generated service's __init functions */ /* === behind the scenes on the generated service's __init functions */
typedef void (*ProtobufCServiceDestroy) (ProtobufCService *service);
typedef void (*ProtobufCServiceDestroy)(ProtobufCService *);
void void
protobuf_c_service_generated_init (ProtobufCService *service, protobuf_c_service_generated_init(
ProtobufCService *service,
const ProtobufCServiceDescriptor *descriptor, const ProtobufCServiceDescriptor *descriptor,
ProtobufCServiceDestroy destroy); ProtobufCServiceDestroy destroy);
void void
protobuf_c_service_invoke_internal(ProtobufCService *service, protobuf_c_service_invoke_internal(
ProtobufCService *service,
unsigned method_index, unsigned method_index,
const ProtobufCMessage *input, const ProtobufCMessage *input,
ProtobufCClosure closure, ProtobufCClosure closure,

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,4 @@
/* --- protobuf-c.h: public protobuf c runtime api --- */ /* --- protobuf-c.h: protobuf c runtime api --- */
/* /*
* Copyright (c) 2008-2013, Dave Benson. All rights reserved. * Copyright (c) 2008-2013, Dave Benson. All rights reserved.
@ -43,12 +43,19 @@
# define PROTOBUF_C_END_DECLS # define PROTOBUF_C_END_DECLS
#endif #endif
#if !defined(PROTOBUF_C_NO_DEPRECATED) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)) #define PROTOBUF_C_ASSERT(condition) assert(condition)
#define PROTOBUF_C_DEPRECATED __attribute__((__deprecated__)) #define PROTOBUF_C_ASSERT_NOT_REACHED() assert(0)
#if !defined(PROTOBUF_C_NO_DEPRECATED)
# if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
# define PROTOBUF_C_DEPRECATED __attribute__((__deprecated__))
# endif
#else #else
#define PROTOBUF_C_DEPRECATED # define PROTOBUF_C_DEPRECATED
#endif #endif
#define PROTOBUF_C_OFFSETOF(struct, member) offsetof(struct, member)
/* The version of protobuf-c you are compiling against. */ /* The version of protobuf-c you are compiling against. */
#define PROTOBUF_C_MAJOR 0 #define PROTOBUF_C_MAJOR 0
#define PROTOBUF_C_MINOR 16 #define PROTOBUF_C_MINOR 16
@ -57,14 +64,14 @@
extern unsigned protobuf_c_major; extern unsigned protobuf_c_major;
extern unsigned protobuf_c_minor; extern unsigned protobuf_c_minor;
/* Define int32_t, int64_t, uint32_t, uint64_t, uint8_t. /*
* Define int32_t, int64_t, uint32_t, uint64_t, uint8_t.
Usually, just include <inttypes.h> to do the work. *
XXX: should we use stdint.h? * Usually, just include <inttypes.h> to do the work.
* XXX: should we use stdint.h?
*/ */
#ifndef PROTOBUF_C_SKIP_INTTYPES_H #ifndef PROTOBUF_C_SKIP_INTTYPES_H
# if defined(_MSC_VER) # if defined(_MSC_VER) /* For MS Visual Studio, define the types ourselves */
/* On windows, in ms visual studio, define the types ourselves */
# define int32_t signed __int32 # define int32_t signed __int32
# define INT32_MIN _I32_MIN # define INT32_MIN _I32_MIN
# define INT32_MAX _I32_MAX # define INT32_MAX _I32_MAX
@ -78,36 +85,32 @@ extern unsigned protobuf_c_minor;
# define UINT64_MIN _UI64_MIN # define UINT64_MIN _UI64_MIN
# define UINT64_MAX _UI64_MAX # define UINT64_MAX _UI64_MAX
# define uint8_t unsigned char # define uint8_t unsigned char
# else # else /* Use the system inttypes.h */
/* Use the system inttypes.h */
# include <inttypes.h> # include <inttypes.h>
# endif # endif
#endif #endif
#if defined(_WIN32) && defined(PROTOBUF_C_USE_SHARED_LIB) #if defined(_WIN32) && defined(PROTOBUF_C_USE_SHARED_LIB)
# ifdef PROTOBUF_C_EXPORT # ifdef PROTOBUF_C_EXPORT
# define PROTOBUF_C_API __declspec(dllexport) # define PROTOBUF_C_API __declspec(dllexport)
# else # else
# define PROTOBUF_C_API __declspec(dllimport) # define PROTOBUF_C_API __declspec(dllimport)
#endif # endif
#else #else
# define PROTOBUF_C_API # define PROTOBUF_C_API
#endif #endif
PROTOBUF_C_BEGIN_DECLS PROTOBUF_C_BEGIN_DECLS
typedef enum typedef int protobuf_c_boolean;
{
typedef enum {
PROTOBUF_C_LABEL_REQUIRED, PROTOBUF_C_LABEL_REQUIRED,
PROTOBUF_C_LABEL_OPTIONAL, PROTOBUF_C_LABEL_OPTIONAL,
PROTOBUF_C_LABEL_REPEATED PROTOBUF_C_LABEL_REPEATED
} ProtobufCLabel; } ProtobufCLabel;
typedef enum typedef enum {
{
PROTOBUF_C_TYPE_INT32, PROTOBUF_C_TYPE_INT32,
PROTOBUF_C_TYPE_SINT32, PROTOBUF_C_TYPE_SINT32,
PROTOBUF_C_TYPE_SFIXED32, PROTOBUF_C_TYPE_SFIXED32,
@ -128,16 +131,8 @@ typedef enum
PROTOBUF_C_TYPE_MESSAGE, PROTOBUF_C_TYPE_MESSAGE,
} ProtobufCType; } ProtobufCType;
typedef int protobuf_c_boolean;
#define PROTOBUF_C_OFFSETOF(struct, member) offsetof(struct, member)
#define PROTOBUF_C_ASSERT(condition) assert(condition)
#define PROTOBUF_C_ASSERT_NOT_REACHED() assert(0)
typedef struct _ProtobufCBinaryData ProtobufCBinaryData; typedef struct _ProtobufCBinaryData ProtobufCBinaryData;
struct _ProtobufCBinaryData struct _ProtobufCBinaryData {
{
size_t len; size_t len;
uint8_t *data; uint8_t *data;
}; };
@ -155,7 +150,8 @@ struct _ProtobufCAllocator
void *allocator_data; void *allocator_data;
}; };
/* This is a configurable allocator. /*
* This is a configurable allocator.
* By default, it uses the system allocator (meaning malloc() and free()). * By default, it uses the system allocator (meaning malloc() and free()).
* This is typically changed to adapt to frameworks that provide * This is typically changed to adapt to frameworks that provide
* some nonstandard allocation functions. * some nonstandard allocation functions.
@ -164,39 +160,42 @@ 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 * This is the function that our default allocators call when they run
terminate your program. */ * out-of-memory. The default behavior of this function is to terminate your
extern PROTOBUF_C_API void (*protobuf_c_out_of_memory) (void); * 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 {
{
void (*append)(ProtobufCBuffer *buffer, void (*append)(ProtobufCBuffer *buffer,
size_t len, size_t len,
const uint8_t *data); const uint8_t *data);
}; };
/* --- enums --- */ /* --- enums --- */
typedef struct _ProtobufCEnumValue ProtobufCEnumValue; typedef struct _ProtobufCEnumValue ProtobufCEnumValue;
typedef struct _ProtobufCEnumValueIndex ProtobufCEnumValueIndex; typedef struct _ProtobufCEnumValueIndex ProtobufCEnumValueIndex;
typedef struct _ProtobufCEnumDescriptor ProtobufCEnumDescriptor; typedef struct _ProtobufCEnumDescriptor ProtobufCEnumDescriptor;
/* ProtobufCEnumValue: this represents a single value of /*
* an enumeration. * ProtobufCEnumValue: this represents a single value of an enumeration.
* 'name' is the string identifying this value, as given in the .proto file. * 'name' is the string identifying this value, as given in the .proto file.
* 'c_name' is the full name of the C enumeration value. * 'c_name' is the full name of the C enumeration value.
* 'value' is the number assigned to this value, as given in the .proto file. * 'value' is the number assigned to this value, as given in the .proto file.
*/ */
struct _ProtobufCEnumValue struct _ProtobufCEnumValue {
{
const char *name; const char *name;
const char *c_name; const char *c_name;
int value; int value;
}; };
/* ProtobufCEnumDescriptor: the represents the enum as a whole, /*
* with all its values. * ProtobufCEnumDescriptor: represents the enum as a whole, with all its values.
*
* 'magic' is a code we check to ensure that the api is used correctly. * 'magic' is a code we check to ensure that the api is used correctly.
* 'name' is the qualified name (e.g. "namespace.Type"). * 'name' is the qualified name (e.g. "namespace.Type").
* 'short_name' is the unqualified name ("Type"), as given in the .proto file. * 'short_name' is the unqualified name ("Type"), as given in the .proto file.
@ -208,12 +207,11 @@ struct _ProtobufCEnumValue
* *
* The rest of the values are private essentially. * The rest of the values are private essentially.
* *
* see also: Use protobuf_c_enum_descriptor_get_value_by_name() * See also: Use protobuf_c_enum_descriptor_get_value_by_name()
* and protobuf_c_enum_descriptor_get_value() to efficiently * and protobuf_c_enum_descriptor_get_value() to efficiently
* lookup values in the descriptor. * lookup values in the descriptor.
*/ */
struct _ProtobufCEnumDescriptor struct _ProtobufCEnumDescriptor {
{
uint32_t magic; uint32_t magic;
const char *name; const char *name;
@ -240,12 +238,15 @@ struct _ProtobufCEnumDescriptor
}; };
/* --- messages --- */ /* --- messages --- */
typedef struct _ProtobufCMessageDescriptor ProtobufCMessageDescriptor; typedef struct _ProtobufCMessageDescriptor ProtobufCMessageDescriptor;
typedef struct _ProtobufCFieldDescriptor ProtobufCFieldDescriptor; typedef struct _ProtobufCFieldDescriptor ProtobufCFieldDescriptor;
typedef struct _ProtobufCMessage ProtobufCMessage; typedef struct _ProtobufCMessage ProtobufCMessage;
typedef void (*ProtobufCMessageInit)(ProtobufCMessage *); typedef void (*ProtobufCMessageInit)(ProtobufCMessage *);
/* ProtobufCFieldDescriptor: description of a single field
* in a message. /*
* ProtobufCFieldDescriptor: description of a single field in a message.
*
* 'name' is the name of the field, as given in the .proto file. * 'name' is the name of the field, as given in the .proto file.
* 'id' is the code representing the field, as given in the .proto file. * 'id' is the code representing the field, as given in the .proto file.
* 'label' is one of PROTOBUF_C_LABEL_{REQUIRED,OPTIONAL,REPEATED} * 'label' is one of PROTOBUF_C_LABEL_{REQUIRED,OPTIONAL,REPEATED}
@ -263,8 +264,7 @@ typedef void (*ProtobufCMessageInit)(ProtobufCMessage *);
* 'packed' is only for REPEATED fields (it is 0 otherwise); this is if * 'packed' is only for REPEATED fields (it is 0 otherwise); this is if
* the repeated fields is marked with the 'packed' options. * the repeated fields is marked with the 'packed' options.
*/ */
struct _ProtobufCFieldDescriptor struct _ProtobufCFieldDescriptor {
{
const char *name; const char *name;
uint32_t id; uint32_t id;
ProtobufCLabel label; ProtobufCLabel label;
@ -272,14 +272,16 @@ struct _ProtobufCFieldDescriptor
unsigned quantifier_offset; unsigned quantifier_offset;
unsigned offset; unsigned offset;
const void *descriptor; /* for MESSAGE and ENUM types */ const void *descriptor; /* for MESSAGE and ENUM types */
const void *default_value; /* or NULL if no default-value */ const void *default_value; /* can be NULL */
protobuf_c_boolean packed; protobuf_c_boolean packed;
unsigned reserved_flags; unsigned reserved_flags;
void *reserved2; void *reserved2;
void *reserved3; void *reserved3;
}; };
/* ProtobufCMessageDescriptor: description of a message.
/*
* ProtobufCMessageDescriptor: description of a message.
* *
* 'magic' is a code we check to ensure that the api is used correctly. * 'magic' is a code we check to ensure that the api is used correctly.
* 'name' is the qualified name (e.g. "namespace.Type"). * 'name' is the qualified name (e.g. "namespace.Type").
@ -293,8 +295,7 @@ struct _ProtobufCFieldDescriptor
* 'fields_sorted_by_name', 'n_field_ranges' and 'field_ranges' * 'fields_sorted_by_name', 'n_field_ranges' and 'field_ranges'
* are used for looking up fields by name and id. (private) * are used for looking up fields by name and id. (private)
*/ */
struct _ProtobufCMessageDescriptor struct _ProtobufCMessageDescriptor {
{
uint32_t magic; uint32_t magic;
const char *name; const char *name;
@ -319,78 +320,83 @@ struct _ProtobufCMessageDescriptor
void *reserved3; void *reserved3;
}; };
/*
/* ProtobufCMessage: an instance of a message. * ProtobufCMessage: an instance of a message.
* *
* ProtobufCMessage is sort-of a lightweight * ProtobufCMessage is sort of a light-weight base class for all messages.
* base-class for all messages.
* *
* In particular, ProtobufCMessage doesn't have * In particular, ProtobufCMessage doesn't have any allocation policy
* any allocation policy associated with it. * associated with it. That's because it's common to create ProtobufCMessage's
* That's because it is common to create ProtobufCMessage's * on the stack. In fact, that's what we recommend for sending messages
* on the stack. In fact, we that's what we recommend * (because if you just allocate from the stack, then you can't really have a
* for sending messages (because if you just allocate from the * memory leak).
* stack, then you can't really have a memory leak).
* *
* This means that functions like protobuf_c_message_unpack() * This means that functions like protobuf_c_message_unpack() which return a
* which return a ProtobufCMessage must be paired * ProtobufCMessage must be paired with a free function, like
* with a free function, like protobuf_c_message_free_unpacked(). * protobuf_c_message_free_unpacked().
* *
* 'descriptor' gives the locations and types of the members of message * 'descriptor' gives the locations and types of the members of message.
* 'n_unknown_fields' is the number of fields we didn't recognize. * 'n_unknown_fields' is the number of fields we didn't recognize.
* 'unknown_fields' are fields we didn't recognize. * 'unknown_fields' are fields we didn't recognize.
*/ */
typedef struct _ProtobufCMessageUnknownField ProtobufCMessageUnknownField; typedef struct _ProtobufCMessageUnknownField ProtobufCMessageUnknownField;
struct _ProtobufCMessage struct _ProtobufCMessage {
{
const ProtobufCMessageDescriptor *descriptor; const ProtobufCMessageDescriptor *descriptor;
unsigned n_unknown_fields; unsigned n_unknown_fields;
ProtobufCMessageUnknownField *unknown_fields; ProtobufCMessageUnknownField *unknown_fields;
}; };
#define PROTOBUF_C_MESSAGE_INIT(descriptor) { descriptor, 0, NULL } #define PROTOBUF_C_MESSAGE_INIT(descriptor) { descriptor, 0, NULL }
/* To pack a message: you have two options: /*
(1) you can compute the size of the message * To pack a message: you have two options:
using protobuf_c_message_get_packed_size() * (1) you can compute the size of the message using
then pass protobuf_c_message_pack() a buffer of * protobuf_c_message_get_packed_size() then pass
that length. * protobuf_c_message_pack() a buffer of that length.
(2) Provide a virtual buffer (a ProtobufCBuffer) to *
accept data as we scan through it. * (2) Provide a virtual buffer (a ProtobufCBuffer) to
* accept data as we scan through it.
*/ */
PROTOBUF_C_API size_t protobuf_c_message_get_packed_size(const ProtobufCMessage *message); PROTOBUF_C_API size_t
PROTOBUF_C_API size_t protobuf_c_message_pack (const ProtobufCMessage *message, protobuf_c_message_get_packed_size(const ProtobufCMessage *);
uint8_t *out);
PROTOBUF_C_API size_t protobuf_c_message_pack_to_buffer (const ProtobufCMessage *message, PROTOBUF_C_API size_t
ProtobufCBuffer *buffer); protobuf_c_message_pack(const ProtobufCMessage *, uint8_t *out);
PROTOBUF_C_API size_t
protobuf_c_message_pack_to_buffer(const ProtobufCMessage *, ProtobufCBuffer *);
PROTOBUF_C_API ProtobufCMessage * PROTOBUF_C_API ProtobufCMessage *
protobuf_c_message_unpack (const ProtobufCMessageDescriptor *, protobuf_c_message_unpack(
ProtobufCAllocator *allocator, const ProtobufCMessageDescriptor *,
ProtobufCAllocator *,
size_t len, size_t len,
const uint8_t *data); const uint8_t *data);
PROTOBUF_C_API void protobuf_c_message_free_unpacked (ProtobufCMessage *message,
ProtobufCAllocator *allocator); PROTOBUF_C_API void
protobuf_c_message_free_unpacked(ProtobufCMessage *, ProtobufCAllocator *);
PROTOBUF_C_API protobuf_c_boolean PROTOBUF_C_API protobuf_c_boolean
protobuf_c_message_check (const ProtobufCMessage *message); protobuf_c_message_check(const ProtobufCMessage *);
/* WARNING: 'message' must be a block of memory /*
of size descriptor->sizeof_message. */ * WARNING: 'message' must be a block of memory of size
PROTOBUF_C_API void protobuf_c_message_init (const ProtobufCMessageDescriptor *, * descriptor->sizeof_message.
void *message); */
PROTOBUF_C_API void
protobuf_c_message_init(const ProtobufCMessageDescriptor *, void *message);
/* --- services --- */ /* --- services --- */
typedef struct _ProtobufCMethodDescriptor ProtobufCMethodDescriptor;
typedef struct _ProtobufCServiceDescriptor ProtobufCServiceDescriptor;
struct _ProtobufCMethodDescriptor typedef struct _ProtobufCMethodDescriptor ProtobufCMethodDescriptor;
{ struct _ProtobufCMethodDescriptor {
const char *name; const char *name;
const ProtobufCMessageDescriptor *input; const ProtobufCMessageDescriptor *input;
const ProtobufCMessageDescriptor *output; const ProtobufCMessageDescriptor *output;
}; };
struct _ProtobufCServiceDescriptor
{ typedef struct _ProtobufCServiceDescriptor ProtobufCServiceDescriptor;
struct _ProtobufCServiceDescriptor {
uint32_t magic; uint32_t magic;
const char *name; const char *name;
@ -402,49 +408,52 @@ struct _ProtobufCServiceDescriptor
const unsigned *method_indices_by_name; const unsigned *method_indices_by_name;
}; };
typedef void (*ProtobufCClosure)(const ProtobufCMessage *, void *closure_data);
typedef struct _ProtobufCService ProtobufCService; typedef struct _ProtobufCService ProtobufCService;
typedef void (*ProtobufCClosure)(const ProtobufCMessage *message, struct _ProtobufCService {
void *closure_data);
struct _ProtobufCService
{
const ProtobufCServiceDescriptor *descriptor; const ProtobufCServiceDescriptor *descriptor;
void (*invoke)(ProtobufCService *service, void (*invoke)(ProtobufCService *service,
unsigned method_index, unsigned method_index,
const ProtobufCMessage *input, const ProtobufCMessage *input,
ProtobufCClosure closure, ProtobufCClosure closure,
void *closure_data); void *closure_data);
void (*destroy) (ProtobufCService *service); void (*destroy)(ProtobufCService *service);
}; };
PROTOBUF_C_API void
void protobuf_c_service_destroy (ProtobufCService *); protobuf_c_service_destroy(ProtobufCService *);
/* --- querying the descriptors --- */ /* --- querying the descriptors --- */
PROTOBUF_C_API const ProtobufCEnumValue * PROTOBUF_C_API const ProtobufCEnumValue *
protobuf_c_enum_descriptor_get_value_by_name protobuf_c_enum_descriptor_get_value_by_name(
(const ProtobufCEnumDescriptor *desc, const ProtobufCEnumDescriptor *desc,
const char *name); const char *name);
PROTOBUF_C_API const ProtobufCEnumValue * PROTOBUF_C_API const ProtobufCEnumValue *
protobuf_c_enum_descriptor_get_value protobuf_c_enum_descriptor_get_value(
(const ProtobufCEnumDescriptor *desc, const ProtobufCEnumDescriptor *desc,
int value); int value);
PROTOBUF_C_API const ProtobufCFieldDescriptor * PROTOBUF_C_API const ProtobufCFieldDescriptor *
protobuf_c_message_descriptor_get_field_by_name protobuf_c_message_descriptor_get_field_by_name(
(const ProtobufCMessageDescriptor *desc, const ProtobufCMessageDescriptor *desc,
const char *name); const char *name);
PROTOBUF_C_API const ProtobufCFieldDescriptor * PROTOBUF_C_API const ProtobufCFieldDescriptor *
protobuf_c_message_descriptor_get_field protobuf_c_message_descriptor_get_field(
(const ProtobufCMessageDescriptor *desc, const ProtobufCMessageDescriptor *desc,
unsigned value); unsigned value);
PROTOBUF_C_API const ProtobufCMethodDescriptor * PROTOBUF_C_API const ProtobufCMethodDescriptor *
protobuf_c_service_descriptor_get_method_by_name protobuf_c_service_descriptor_get_method_by_name(
(const ProtobufCServiceDescriptor *desc, const ProtobufCServiceDescriptor *desc,
const char *name); const char *name);
/* --- wire format enums --- */ /* --- wire format enums --- */
typedef enum
{ typedef enum {
PROTOBUF_C_WIRE_TYPE_VARINT, PROTOBUF_C_WIRE_TYPE_VARINT,
PROTOBUF_C_WIRE_TYPE_64BIT, PROTOBUF_C_WIRE_TYPE_64BIT,
PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED, PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED,
@ -454,8 +463,8 @@ typedef enum
} ProtobufCWireType; } ProtobufCWireType;
/* --- unknown message fields --- */ /* --- unknown message fields --- */
struct _ProtobufCMessageUnknownField
{ struct _ProtobufCMessageUnknownField {
uint32_t tag; uint32_t tag;
ProtobufCWireType wire_type; ProtobufCWireType wire_type;
size_t len; size_t len;
@ -463,21 +472,32 @@ struct _ProtobufCMessageUnknownField
}; };
/* --- extra (superfluous) api: trivial buffer --- */ /* --- extra (superfluous) api: trivial buffer --- */
typedef struct _ProtobufCBufferSimple ProtobufCBufferSimple; typedef struct _ProtobufCBufferSimple ProtobufCBufferSimple;
struct _ProtobufCBufferSimple struct _ProtobufCBufferSimple {
{
ProtobufCBuffer base; ProtobufCBuffer base;
size_t alloced; size_t alloced;
size_t len; size_t len;
uint8_t *data; uint8_t *data;
protobuf_c_boolean must_free_data; protobuf_c_boolean must_free_data;
}; };
#define PROTOBUF_C_BUFFER_SIMPLE_INIT(array_of_bytes) \ #define PROTOBUF_C_BUFFER_SIMPLE_INIT(array_of_bytes) \
{ { protobuf_c_buffer_simple_append }, \ { \
sizeof(array_of_bytes), 0, (array_of_bytes), 0 } { protobuf_c_buffer_simple_append }, \
sizeof(array_of_bytes), \
0, \
(array_of_bytes), \
0 \
}
#define PROTOBUF_C_BUFFER_SIMPLE_CLEAR(simp_buf) \ #define PROTOBUF_C_BUFFER_SIMPLE_CLEAR(simp_buf) \
do { if ((simp_buf)->must_free_data) \ do { \
protobuf_c_default_allocator.free (&protobuf_c_default_allocator.allocator_data, (simp_buf)->data); } while (0) if ((simp_buf)->must_free_data) \
protobuf_c_default_allocator.free( \
&protobuf_c_default_allocator.allocator_data, \
(simp_buf)->data); \
} while (0)
#ifdef PROTOBUF_C_PLEASE_INCLUDE_CTYPE #ifdef PROTOBUF_C_PLEASE_INCLUDE_CTYPE
// This type is meant to reduce duplicated logic between // This type is meant to reduce duplicated logic between
@ -488,8 +508,7 @@ struct _ProtobufCBufferSimple
// Because the names are confusing, and because this mechanism tends to be // Because the names are confusing, and because this mechanism tends to be
// seldom used, you have to specifically request // seldom used, you have to specifically request
// this API via #define PROTOBUF_C_PLEASE_INCLUDE_CTYPE. // this API via #define PROTOBUF_C_PLEASE_INCLUDE_CTYPE.
typedef enum typedef enum {
{
PROTOBUF_C_CTYPE_INT32, PROTOBUF_C_CTYPE_INT32,
PROTOBUF_C_CTYPE_UINT32, PROTOBUF_C_CTYPE_UINT32,
PROTOBUF_C_CTYPE_INT64, PROTOBUF_C_CTYPE_INT64,
@ -503,7 +522,7 @@ typedef enum
PROTOBUF_C_CTYPE_MESSAGE, PROTOBUF_C_CTYPE_MESSAGE,
} ProtobufC_CType; } ProtobufC_CType;
extern ProtobufC_CType protobuf_c_type_to_ctype (ProtobufCType type); extern ProtobufC_CType protobuf_c_type_to_ctype(ProtobufCType type);
#define protobuf_c_type_to_ctype(type) \ #define protobuf_c_type_to_ctype(type) \
((ProtobufC_CType)(protobuf_c_type_to_ctype_array[(type)])) ((ProtobufC_CType)(protobuf_c_type_to_ctype_array[(type)]))
#endif #endif
@ -511,7 +530,6 @@ extern ProtobufC_CType protobuf_c_type_to_ctype (ProtobufCType type);
/* ====== private ====== */ /* ====== private ====== */
#include "protobuf-c-private.h" #include "protobuf-c-private.h"
PROTOBUF_C_END_DECLS PROTOBUF_C_END_DECLS
#endif /* __PROTOBUF_C_RUNTIME_H_ */ #endif /* __PROTOBUF_C_RUNTIME_H_ */