Thread: Sharing a Struct with a Library

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    1

    Question Sharing a Struct with a Library

    Hello,

    I am currently trying to modify the OpenSSL library to include a struct that I have implemented. This part works great. I have created a hash table using a doubly linked list, and can insert, find, and everything else perfectly. From inside the library code at least.

    My problem is that, the whole reason I am injecting this code is to be able to access the structure outside of the library in my program that uses Openssl. I am currently doing this with a global struct hash map that is being declared in the OpenSSL library (at global scope) and extern'd back into my program. The flow of control is as follows:

    Library function call
    Library inserts into hash table
    Library function returns
    User Program reads from hash table
    User Program deletes the data from the hash table

    Whenever I run my program the Library inserts perfectly fine, and the program reads perfectly fine, however they both insert, and read from different hash tables! I'm am thoroughly confused over this, and welcome any response. If there is a better way to do this from a shared library than a global, fell free to suggest a new method. I can also post any code that is needed.

    The declaration of the hash table (and a shared threading lock) are right below the includes:

    CMAP * key_map = NULL; //CMAP is a typdef of the struct that I created, and needs to be init'd to NULL as per the UThash documentation which is what I'm using for the hash table
    pthread_rwlock_t lock;

    CMAP is defined as:

    typedef struct c_map{
    pthread_t threadIdKey;
    unsigned char * key;
    int key_len;
    UT_hash_handle hh;
    } CMAP;

    And there are three functions that go along with CMAP:

    CMAP * hash_find(pthread_t threadId);
    int hash_delete(pthread_t threadId);
    int hash_add(pthread_t threadId, int key_len, unsigned char * key);

    The library calls hash_add (which internally calls hash find)
    and the user program calls hash find, and hash delete.

    Thanks in advanced!

    -Sam

  2. #2
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    firstly i would suggest moving your custom CMAP code into separate .c/.h files. make sure the .h contains declarations of your custom struct(s) and the functions to use them. provide the function implementations in the .c file. compile with
    Code:
    gcc cmap.c -c -o cmap.o
    in the openssl file you want to use it, add
    Code:
    #include "cmap.h"
    make sure to link to it when compiling openssl, by adding cmap.o to compile command

    as for sharing the same CMAP struct instance between openssl, i think the easiest way would be to provide an accessor function
    Code:
    cmap* getCurrentCmap() {
      return myCmap;
    }
    where myCmap could be globally defined (better yet, as a member of some other object if possible). you would then add this function definition to the header file for the openssl code file you modified. in your custom project, add the appropriate include so you have the function definition (i'm assuming you're already doing this however). i don't think you need to do anything with extern, using this method.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I'm donating my vector lib.
    By User Name: in forum C Programming
    Replies: 23
    Last Post: 06-24-2010, 06:10 PM
  2. C problem - Create a Personal Library
    By Harliqueen in forum C Programming
    Replies: 33
    Last Post: 04-20-2010, 11:27 PM
  3. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  4. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  5. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 10:24 AM

Tags for this Thread