This is the mail archive of the libffi-discuss@sourceware.org mailing list for the libffi project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Caching the ffi_cif structure, thread-safety vs. stack for the ffi_type** array


If you want to cache the ffi_cif that ffi_prep_cif prepares for a
function that will be called more than once and by different threads,
                                            ^^^^^^^^^^^^^^^^^^^^^^^^
then you can't really use stack for the ffi_type** array.

Otherwise the array might have been destroyed as the stack is given
back, of course (which ain't very thread-safe).

Is this the right way to do it then?

typedef struct {
	void *moredata;
	int cif_cached;
	ffi_cif cif;
	void (*func) (int a, int b);
} SomeCachedInfo;


void 
call_my_function (int a, int b) {

  SomeCachedInfo *info = get_my_function_info (...);
  void *values[2];

  if (!info->cif_cached) {
    ffi_type **atypes = malloc (sizeof (ffi_type*) * 2); 
    ffi_type  *rtype  = &ffi_type_something;

    /* prepare atypes*/
  
    ffi_prep_cif (&info->cif, FFI_DEFAULT_ABI, 
                  2, rtype, atypes);

    info->cif_cached = 1;
  }

  /* prepare values with a and b */
 
  ffi_call (&info->cif, info->func, rtype, valies);
}

void 
some_cached_info_free (SomeCachedInfo *info) {

	if (info->cif_cached)
		free (info->cif.arg_types);
	free (info);
}


-- 
Philip Van Hoof, freelance software developer
home: me at pvanhoof dot be 
gnome: pvanhoof at gnome dot org 
http://pvanhoof.be/blog
http://codeminded.be


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]