cprops - c prototyping tools


man page index

doxygen documentation

code samples


implementing a cp_dbms driver

cpsvc README


SourceForge Project page

A few things to pay attention to if you're using cprops on unix and ever intend to port your code to windows.
  • handling memory : on windows, you can't free memory that was allocated by a DLL except through a function in that same DLL. If your code allocates memory through a cprops API function, you must release it through another API function. That's what cp_free does. Example:

    (1) #include <cprops/util.h> /* intdup */
    (3) void main()
    (4) {
    (5)     int i = 1; 
    (6)     int *j = intdup(&i);
    (7)     cp_free(i);
    (8) }
    On unix, cp_free() is a synonym for free(). If line (7) called free() rather than cp_free(), it would work fine on unix, but cause a debug assertion or exception on windows. Less trivial example:
    (1) cp_hashtable *h = 
    (2)     cp_hashtable_create_by_option(COLLECTION_MODE_DEEP, 100,
    (3)                                   cp_hash_string, strcmp, 
    (4)                                   NULL, free, NULL, free);
    (6) cp_hashtable_put(h, strdup("key"), strdup("value");
    (8) cp_hashtable_destroy(h);
    When you run this code on windows, your machine may beep and offer to send a bug report (click 'Don't Send'). This happens because you allocate memory in the main program with strdup(), then attempt to release it in the call to cp_hashtable_destroy(), i.e. in the DLL.
    In this case the problem could be solved by creating the hashtable with COLLECTION_MODE_COPY and setting strdup() as the copy function. A more general solution would be to perform all allocations with cp_malloc(), cp_calloc() and cp_realloc(), and always release memory with cp_free().

  • threads and synchronization: cprops provides a pthread-like api. On unix platforms these calls are mapped directly to pthread functions. On windows the win32 api is used to emulate pthread-like behavior.
    #include <cprops/collection.h>
    typedef HANDLE cp_thread; /* thread object */
    typedef HANDLE *cp_lock;  /* lock object */
    typedef HANDLE *cp_mutex; /* mutex object */
    typedef struct cp_cond;   /* condition variable */
    int cp_thread_create(cp_thread t, 
                         void *attributes, 
                         void *(*fn)(void *), 
                         void *prm);
    void cp_thread_join(cp_thread t, void **exit);
    void cp_thread_detach(cp_thread t);
    cp_thread cp_thread_self();
    int cp_thread_equal(cp_thread p, cp_thread q);
    int cp_mutex_init(cp_mutex *mutex, void *attr);
    int cp_mutex_lock(cp_mutex *mutex);
    int cp_mutex_unlock(cp_mutex *mutex);
    void cp_mutex_destroy(cp_mutex *mutex);
    int cp_cond_init(cp_cond *cv, const void *attr); 
    int cp_cond_wait(cp_cond *cv, cp_mutex *mutex);
    int cp_cond_signal(cp_cond *cv);
    int cp_cond_broadcast(cp_cond *cv);
    int cp_cond_destroy(cp_cond *cv);
    int cp_lock_init(cp_lock *lock, void *attr);
    int cp_lock_rdlock(cp_lock *lock);
    int cp_lock_wrlock(cp_lock *lock);
    int cp_lock_unlock(cp_lock *lock);
    int cp_lock_destroy(cp_lock *lock);
  • the following functions are emulated for windows:
    #include <cprops/util.h>
    char *strndup(char *src, int maxlen);
    int gettimeofday(struct timeval *res, struct timezone *tz);
    int getopt(int argc, char *argv[], char *fmt);
    char *inet_ntop(int af, const void *src, char *dst, size_t cnt);
    void *dlopen(char *file, int mode);
    int dlclose(void *handle);
    void *dlsym(void *handle, char *name);
    char *dlerror();
    getcwd is defined to _getcwd
    strcasecmp is defined to _stricmp
SourceForge.net Logo