mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-10-16 18:56:54 +02:00
msgpack template: architecture specific endian conversion
This commit is contained in:
10
c/object.c
10
c/object.c
@@ -18,7 +18,17 @@
|
||||
#include "msgpack/object.h"
|
||||
#include "msgpack/pack.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#ifndef _MSC_VER
|
||||
#include <inttypes.h>
|
||||
#else
|
||||
#ifndef PRIu64
|
||||
#define PRIu64 "I64u"
|
||||
#endif
|
||||
#ifndef PRIi64
|
||||
#define PRIi64 "I64d"
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
int msgpack_pack_object(msgpack_packer* pk, msgpack_object d)
|
||||
|
@@ -19,9 +19,7 @@
|
||||
#define MSGPACK_OBJECT_H__
|
||||
|
||||
#include "msgpack/zone.h"
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include "msgpack/sys.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
4
c/pack.h
4
c/pack.h
@@ -18,11 +18,9 @@
|
||||
#ifndef MSGPACK_PACK_H__
|
||||
#define MSGPACK_PACK_H__
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include "msgpack/pack_define.h"
|
||||
#include "msgpack/object.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
|
35
c/unpack.c
35
c/unpack.c
@@ -101,7 +101,7 @@ static inline int template_callback_array(unpack_user* u, unsigned int n, msgpac
|
||||
{
|
||||
o->type = MSGPACK_OBJECT_ARRAY;
|
||||
o->via.array.size = 0;
|
||||
o->via.array.ptr = msgpack_zone_malloc(u->z, n*sizeof(msgpack_object));
|
||||
o->via.array.ptr = (msgpack_object*)msgpack_zone_malloc(u->z, n*sizeof(msgpack_object));
|
||||
if(o->via.array.ptr == NULL) { return -1; }
|
||||
return 0;
|
||||
}
|
||||
@@ -142,30 +142,47 @@ static inline int template_callback_raw(unpack_user* u, const char* b, const cha
|
||||
#define CTX_REFERENCED(mpac) CTX_CAST((mpac)->ctx)->user.referenced
|
||||
|
||||
|
||||
static const size_t COUNTER_SIZE = sizeof(unsigned int);
|
||||
#ifndef _MSC_VER
|
||||
typedef unsigned int counter_t;
|
||||
#else
|
||||
typedef long counter_t;
|
||||
#endif
|
||||
|
||||
#define COUNTER_SIZE (sizeof(volatile counter_t))
|
||||
|
||||
|
||||
static inline void init_count(void* buffer)
|
||||
{
|
||||
*(volatile unsigned int*)buffer = 1;
|
||||
*(volatile counter_t*)buffer = 1;
|
||||
}
|
||||
|
||||
static inline void decl_count(void* buffer)
|
||||
{
|
||||
//if(--*(unsigned int*)buffer == 0) {
|
||||
if(__sync_sub_and_fetch((unsigned int*)buffer, 1) == 0) {
|
||||
// atomic if(--*(counter_t*)buffer == 0) { free(buffer); }
|
||||
if(
|
||||
#ifndef _MSC_VER
|
||||
__sync_sub_and_fetch((counter_t*)buffer, 1) == 0
|
||||
#else
|
||||
InterlockedDecrement((volatile counter_t*)&buffer) == 0
|
||||
#endif
|
||||
) {
|
||||
free(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void incr_count(void* buffer)
|
||||
{
|
||||
//++*(unsigned int*)buffer;
|
||||
__sync_add_and_fetch((unsigned int*)buffer, 1);
|
||||
// atomic ++*(counter_t*)buffer;
|
||||
#ifndef _MSC_VER
|
||||
__sync_add_and_fetch((counter_t*)buffer, 1);
|
||||
#else
|
||||
InterlockedIncrement((volatile counter_t*)&buffer);
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline unsigned int get_count(void* buffer)
|
||||
static inline counter_t get_count(void* buffer)
|
||||
{
|
||||
return *(volatile unsigned int*)buffer;
|
||||
return *(volatile counter_t*)buffer;
|
||||
}
|
||||
|
||||
|
||||
|
@@ -15,13 +15,11 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef msgpack_unpacker_H__
|
||||
#define msgpack_unpacker_H__
|
||||
#ifndef MSGPACK_UNPACKER_H__
|
||||
#define MSGPACK_UNPACKER_H__
|
||||
|
||||
#include "msgpack/zone.h"
|
||||
#include "msgpack/object.h"
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
|
@@ -19,7 +19,16 @@
|
||||
#define MSGPACK_VREFBUFFER_H__
|
||||
|
||||
#include "msgpack/zone.h"
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <sys/uio.h>
|
||||
#else
|
||||
struct iovec {
|
||||
void *iov_base;
|
||||
size_t iov_len;
|
||||
};
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
|
Reference in New Issue
Block a user