Skip to content

Commit

Permalink
checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
nelhage committed Dec 3, 2012
1 parent 41015a2 commit f948395
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 21 deletions.
9 changes: 9 additions & 0 deletions _zephyr.pxd
Expand Up @@ -102,3 +102,12 @@ cdef extern from "stdlib.h":
cdef extern from "string.h":
void * memset(void *, int, unsigned int)

cdef extern from "pool.h":
ctypedef struct object_pool:
void **objects
size_t alloc
size_t count

void object_pool_init(object_pool *pool)
void object_pool_append(object_pool *pool, object obj)
void object_pool_free(object_pool *pool)
39 changes: 21 additions & 18 deletions _zephyr.pyx
Expand Up @@ -13,12 +13,6 @@ cdef object _string_c2p(char * string):
else:
return string

cdef char * _string_p2c(object string) except *:
if string is None:
return NULL
else:
return string

class ZUid(object):
"""
A per-transaction unique ID for zephyrs
Expand All @@ -28,7 +22,7 @@ class ZUid(object):
self.address = ''
self.time = 0

cdef void _ZUid_c2p(ZUnique_Id_t * uid, object p_uid):
cdef void _ZUid_c2p(ZUnique_Id_t * uid, object p_uid) except *:
p_uid.address = inet_ntoa(uid.zuid_addr)
p_uid.time = uid.tv.tv_sec + (uid.tv.tv_usec / 100000.0)

Expand All @@ -37,6 +31,13 @@ cdef void _ZUid_p2c(object uid, ZUnique_Id_t * c_uid) except *:
c_uid.tv.tv_usec = int(uid.time)
c_uid.tv.tv_usec = int((uid.time - c_uid.tv.tv_usec) * 100000)

cdef char * _string_p2c(object_pool *pool, object string) except *:
if string is None:
return NULL
else:
object_pool_append(pool, string);
return string

class ZNotice(object):
"""
A zephyr message
Expand Down Expand Up @@ -70,8 +71,10 @@ class ZNotice(object):
message = property(getmessage, setmessage)

def send(self):
cdef object_pool pool
cdef ZNotice_t notice
_ZNotice_p2c(self, &notice)
object_pool_init(&pool)
_ZNotice_p2c(self, &notice, &pool)

original_message = self.message

Expand Down Expand Up @@ -109,7 +112,7 @@ cdef void _ZNotice_c2p(ZNotice_t * notice, object p_notice) except *:
else:
p_notice.message = PyString_FromStringAndSize(notice.z_message, notice.z_message_len)

cdef void _ZNotice_p2c(object notice, ZNotice_t * c_notice) except *:
cdef void _ZNotice_p2c(object notice, ZNotice_t * c_notice, object_pool *pool) except *:
memset(c_notice, 0, sizeof(ZNotice_t))

c_notice.z_kind = notice.kind
Expand All @@ -121,22 +124,22 @@ cdef void _ZNotice_p2c(object notice, ZNotice_t * c_notice) except *:
c_notice.z_port = notice.port
c_notice.z_auth = int(notice.auth)

c_notice.z_class = _string_p2c(notice.cls)
c_notice.z_class_inst = _string_p2c(notice.instance)
c_notice.z_recipient = _string_p2c(notice.recipient)
c_notice.z_sender = _string_p2c(notice.sender)
c_notice.z_opcode = _string_p2c(notice.opcode)
c_notice.z_default_format = _string_p2c(notice.format)
c_notice.z_class = _string_p2c(pool, notice.cls)
c_notice.z_class_inst = _string_p2c(pool, notice.instance)
c_notice.z_recipient = _string_p2c(pool, notice.recipient)
c_notice.z_sender = _string_p2c(pool, notice.sender)
c_notice.z_opcode = _string_p2c(pool, notice.opcode)
c_notice.z_default_format = _string_p2c(pool, notice.format)
c_notice.z_num_other_fields = len(notice.other_fields)
for i in range(c_notice.z_num_other_fields):
c_notice.z_other_fields[i] = _string_p2c(notice.other_fields[i])
c_notice.z_other_fields[i] = _string_p2c(pool, notice.other_fields[i])

if isinstance(notice.message, unicode):
notice.encoded_message = notice.message.encode('utf-8')
else:
notice.encoded_message = notice.message

c_notice.z_message = _string_p2c(notice.encoded_message)
c_notice.z_message = _string_p2c(pool, notice.encoded_message)
c_notice.z_message_len = len(notice.encoded_message)

def initialize():
Expand Down Expand Up @@ -245,7 +248,7 @@ def getSubscriptions():
__error(errno)

subs = []
for i in xrange(num):
for i in range(num):
subs.append((csubs[i].zsub_class, csubs[i].zsub_classinst, csubs[i].zsub_recipient))
return subs
finally:
Expand Down
29 changes: 29 additions & 0 deletions pool.c
@@ -0,0 +1,29 @@
#include "Python.h"

#include <stdlib.h>

#include "pool.h"

void object_pool_init(struct object_pool *pool) {
pool->objects = NULL;
pool->count = pool->alloc = 0;
}

void object_pool_append(struct object_pool *pool, PyObject *obj) {
if (pool->count == pool->alloc) {
size_t new_alloc = pool->alloc ? 2 * pool->alloc : 8;
pool->objects = realloc(pool->objects, new_alloc * sizeof(*pool->objects));
pool->alloc = new_alloc;
}
pool->objects[pool->count++] = obj;
Py_INCREF(obj);
}

void object_pool_free(struct object_pool *pool) {
int i;
for (i = 0; i < pool->count; i++) {
Py_DECREF(pool->objects[i]);
}
free(pool->objects);
object_pool_init(pool);
}
9 changes: 9 additions & 0 deletions pool.h
@@ -0,0 +1,9 @@
typedef struct object_pool {
void **objects;
size_t count;
size_t alloc;
} object_pool;

void object_pool_init(struct object_pool *pool);
void object_pool_append(struct object_pool *pool, PyObject *obj);
void object_pool_free(struct object_pool *pool);
8 changes: 5 additions & 3 deletions setup.py
Expand Up @@ -16,8 +16,10 @@
py_modules=['zephyr'],
ext_modules=[
Extension("_zephyr",
["_zephyr.pyx"],
libraries=["zephyr"])
["_zephyr.pyx", "pool.c"],
libraries=["zephyr"],
extra_compile_args=["-g"],
extra_link_args=["-g"])
],
cmdclass= {"build_ext": build_ext}
cmdclass= {"build_ext": build_ext},
)

0 comments on commit f948395

Please sign in to comment.