1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
|
/* -*- Mode: c; c-basic-offset: 2 -*-
*
* rdf_init.c - Redland library initialisation / termination
*
* Copyright (C) 2000-2007, David Beckett http://purl.org/net/dajobe/
* Copyright (C) 2000-2005, University of Bristol, UK http://www.bristol.ac.uk/
*
* This package is Free Software and part of Redland http://librdf.org/
*
* It is licensed under the following three licenses as alternatives:
* 1. GNU Lesser General Public License (LGPL) V2.1 or any newer version
* 2. GNU General Public License (GPL) V2 or any newer version
* 3. Apache License, V2.0 or any newer version
*
* You may not use this file except in compliance with at least one of
* the above three licenses.
*
* See LICENSE.html or LICENSE.txt at the top of this package for the
* complete terms and further detail along with the license texts for
* the licenses in COPYING.LIB, COPYING and LICENSE-2.0.txt respectively.
*
*
*/
#ifdef HAVE_CONFIG_H
#include <rdf_config.h>
#endif
#ifdef WIN32
#include <win32_rdf_config.h>
#endif
#include <stdio.h>
#include <string.h>
#ifdef WITH_THREADS
#include <pthread.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h> /* for abort() as used in errors */
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h> /* for getpid() */
#endif
/* for gettimeofday */
#if TIME_WITH_SYS_TIME
#include <sys/time.h>
#include <time.h>
#else
#if HAVE_SYS_TIME_H
#include <sys/time.h>
#else
#include <time.h>
#endif
#endif
#include <redland.h>
/* for getpid */
#include <sys/types.h>
#ifdef HAVE_UNISTD
#include <unistd.h>
#endif
#ifdef MODULAR_LIBRDF
#include <ltdl.h>
#endif
#ifndef STANDALONE
const char * const librdf_short_copyright_string = "Copyright 2000-2007 David Beckett. Copyright 2000-2005 University of Bristol";
const char * const librdf_copyright_string = "Copyright (C) 2000-2007 David Beckett - http://purl.org/net/dajobe/\nCopyright (C) 2000-2005 University of Bristol - http://www.bristol.ac.uk/";
/**
* librdf_version_string:
*
* Library full version as a string.
*
* See also #librdf_version_decimal.
*/
const char * const librdf_version_string = VERSION;
/**
* librdf_version_major:
*
* Library major version number as a decimal integer.
*/
const unsigned int librdf_version_major = LIBRDF_VERSION_MAJOR;
/**
* librdf_version_minor:
*
* Library minor version number as a decimal integer.
*/
const unsigned int librdf_version_minor = LIBRDF_VERSION_MINOR;
/**
* librdf_version_release:
*
* Library release version number as a decimal integer.
*/
const unsigned int librdf_version_release = LIBRDF_VERSION_RELEASE;
/**
* librdf_version_decimal:
*
* Library full version as a decimal integer.
*
* See also #librdf_version_string.
*/
const unsigned int librdf_version_decimal = LIBRDF_VERSION_DECIMAL;
/**
* librdf_new_world:
*
* Create a new Redland execution environment.
*
* Once this constructor is called to build a #librdf_world object
* several functions may be called to set some parameters such as
* librdf_world_set_error(), librdf_world_set_warning(),
* librdf_world_set_logger(), librdf_world_set_digest(),
* librdf_world_set_feature().
*
* The world object needs initializing using librdf_world_open()
* whether or not the above functions are called. It will be
* automatically called by all object constructors in Redland 1.0.6
* or later, but for earlier versions it MUST be called before using
* any other part of Redland.
*
* Returns: a new #librdf_world or NULL on failure
*/
librdf_world*
librdf_new_world(void) {
librdf_world *world;
#ifdef HAVE_GETTIMEOFDAY
struct timeval tv;
struct timezone tz;
#endif
world=(librdf_world*)LIBRDF_CALLOC(librdf_world, sizeof(librdf_world), 1);
if(!world)
return NULL;
#ifdef HAVE_GETTIMEOFDAY
if(!gettimeofday(&tv, &tz)) {
world->genid_base=tv.tv_sec;
} else
world->genid_base=1;
#else
world->genid_base=1;
#endif
world->genid_counter=1;
#ifdef MODULAR_LIBRDF
world->ltdl_opened = !(lt_dlinit());
if (world->ltdl_opened)
lt_dlsetsearchpath(LIBRDF_MODULE_DIR);
else
LIBRDF_DEBUG1("lt_dlinit() failed\n");
#endif
return world;
}
/**
* librdf_free_world:
* @world: redland world object
*
* Terminate the library and frees all allocated resources.
**/
void
librdf_free_world(librdf_world *world)
{
/* NOTE: raptor is always initialised as a parser and may
* be also used as a serializer, but it is NOT finished
* in the serializer_raptor registration. Therefore, always
* keep the parser class finishing after the serializer.
*/
librdf_finish_serializer(world);
librdf_finish_parser(world);
librdf_finish_storage(world);
librdf_finish_query(world);
librdf_finish_model(world);
librdf_finish_statement(world);
librdf_finish_concepts(world);
librdf_finish_node(world);
librdf_finish_uri(world);
librdf_finish_hash(world);
librdf_finish_digest(world);
#ifdef WITH_THREADS
if (world->mutex)
{
pthread_mutex_destroy(world->statements_mutex);
SYSTEM_FREE(world->statements_mutex);
world->statements_mutex = NULL;
pthread_mutex_destroy(world->nodes_mutex);
SYSTEM_FREE(world->nodes_mutex);
world->nodes_mutex = NULL;
pthread_mutex_destroy(world->mutex);
SYSTEM_FREE(world->mutex);
world->mutex = NULL;
}
#endif
#ifdef MODULAR_LIBRDF
if (world->ltdl_opened)
lt_dlexit();
#endif
LIBRDF_FREE(librdf_world, world);
}
/**
* librdf_world_init_mutex:
* @world: redland world object
*
* INTERNAL - Create the world mutex.
*/
void
librdf_world_init_mutex(librdf_world* world)
{
#ifdef WITH_THREADS
world->mutex = (pthread_mutex_t *) SYSTEM_MALLOC(sizeof(pthread_mutex_t));
pthread_mutex_init(world->mutex, NULL);
world->nodes_mutex = (pthread_mutex_t *) SYSTEM_MALLOC(sizeof(pthread_mutex_t));
pthread_mutex_init(world->nodes_mutex, NULL);
world->statements_mutex = (pthread_mutex_t *) SYSTEM_MALLOC(sizeof(pthread_mutex_t));
pthread_mutex_init(world->statements_mutex, NULL);
#else
#endif
}
/**
* librdf_world_open:
* @world: redland world object
*
* Open a created redland world environment.
**/
void
librdf_world_open(librdf_world *world)
{
if(world->opened++)
return;
librdf_world_init_mutex(world);
/* Digests first, lots of things use these */
librdf_init_digest(world);
/* Hash next, needed for URIs */
librdf_init_hash(world);
librdf_init_uri(world);
librdf_init_node(world);
librdf_init_concepts(world);
librdf_init_statement(world);
librdf_init_model(world);
librdf_init_storage(world);
/* NOTE: raptor is always initialised as a parser and may
* be also used as a serializer, but it is NOT initialised
* in the serializer_raptor registration. Therefore, always
* keep the parser class initialising before the serializer.
*/
librdf_init_parser(world);
librdf_init_serializer(world);
/* NOTE: Since initialising rasqal calls raptor to create URIs,
* rasqal must be initialised after raptor so that the raptor URI
* implementation is changed with raptor_uri_set_handler() in
* librdf_parser_raptor_constructor() before rasqal tries to create
* URIs.
*
* Otherwise URIs would be allocated with raptor as char* strings
* and then attempted to be freed later as librdf_uri*. Which
* would be bad.
*/
librdf_init_query(world);
}
/**
* librdf_world_set_error:
* @world: redland world object
* @user_data: user data to pass to function
* @error_handler: pointer to the function
*
* Set the world error handling function.
*
* The function will receive callbacks when the world fails.
* librdf_world_set_logger() provides richer access to all log messages
* and should be used in preference.
**/
void
librdf_world_set_error(librdf_world* world, void *user_data,
librdf_log_level_func error_handler)
{
world->error_user_data=user_data;
world->error_handler=error_handler;
}
/**
* librdf_world_set_warning:
* @world: redland world object
* @user_data: user data to pass to function
* @warning_handler: pointer to the function
*
* Set the world warning handling function.
*
* The function will receive callbacks when the world gives a warning.
* librdf_world_set_logger() provides richer access to all log messages
* and should be used in preference.
**/
void
librdf_world_set_warning(librdf_world* world, void *user_data,
librdf_log_level_func warning_handler)
{
world->warning_user_data=user_data;
world->warning_handler=warning_handler;
}
/**
* librdf_world_set_logger:
* @world: redland world object
* @user_data: user data to pass to function
* @log_handler: pointer to the function
*
* Set the world log handling function.
*
* The function will receive callbacks when redland generates a log message
**/
void
librdf_world_set_logger(librdf_world* world, void *user_data,
librdf_log_func log_handler)
{
world->log_user_data=user_data;
world->log_handler=log_handler;
}
/**
* librdf_world_set_digest:
* @world: redland world object
* @name: Digest factory name
*
* Set the default content digest name.
*
* Sets the digest factory for various modules that need to make
* digests of their objects.
*/
void
librdf_world_set_digest(librdf_world* world, const char *name) {
world->digest_factory_name=(char*)name;
}
/**
* librdf_world_get_feature:
* @world: #librdf_world object
* @feature: #librdf_uri feature property
*
* Get the value of a world feature.
*
* Return value: new #librdf_node feature value or NULL if no such feature
* exists or the value is empty.
**/
librdf_node*
librdf_world_get_feature(librdf_world* world, librdf_uri *feature)
{
return NULL; /* none retrievable */
}
/**
* librdf_world_set_feature:
* @world: #librdf_world object
* @feature: #librdf_uri feature property
* @value: #librdf_node feature property value
*
* Set the value of a world feature.
*
* Return value: non 0 on failure (negative if no such feature)
**/
int
librdf_world_set_feature(librdf_world* world, librdf_uri* feature,
librdf_node* value)
{
librdf_uri* genid_base=librdf_new_uri(world, (const unsigned char*)LIBRDF_WORLD_FEATURE_GENID_BASE);
librdf_uri* genid_counter=librdf_new_uri(world, (const unsigned char*)LIBRDF_WORLD_FEATURE_GENID_COUNTER);
int rc= -1;
if(librdf_uri_equals(feature, genid_base)) {
if(!librdf_node_is_resource(value))
rc=1;
else {
int i=atoi((const char*)librdf_node_get_literal_value(value));
if(i<1)
i=1;
#ifdef WITH_THREADS
pthread_mutex_lock(world->mutex);
#endif
world->genid_base=1;
#ifdef WITH_THREADS
pthread_mutex_unlock(world->mutex);
#endif
rc=0;
}
} else if(librdf_uri_equals(feature, genid_counter)) {
if(!librdf_node_is_resource(value))
rc=1;
else {
int i=atoi((const char*)librdf_node_get_literal_value(value));
if(i<1)
i=1;
#ifdef WITH_THREADS
pthread_mutex_lock(world->mutex);
#endif
world->genid_counter=1;
#ifdef WITH_THREADS
pthread_mutex_unlock(world->mutex);
#endif
rc=0;
}
}
librdf_free_uri(genid_base);
librdf_free_uri(genid_counter);
return rc;
}
/* Internal */
unsigned char*
librdf_world_get_genid(librdf_world* world)
{
int id, tmpid, counter, tmpcounter, pid, tmppid;
int length;
unsigned char *buffer;
/* This is read-only and thread safe */
tmpid= (id= world->genid_base);
#ifdef WITH_THREADS
pthread_mutex_lock(world->mutex);
#endif
tmpcounter=(counter=world->genid_counter++);
#ifdef WITH_THREADS
pthread_mutex_unlock(world->mutex);
#endif
/* Add the process ID to the seed to differentiate between
* simultaneously executed child processes.
*/
pid=(int) getpid();
if(!pid)
pid=1;
tmppid=pid;
length=7; /* min length 1 + "r" + min length 1 + "r" + min length 1 + "r" \0 */
while(tmpid/=10)
length++;
while(tmpcounter/=10)
length++;
while(tmppid/=10)
length++;
buffer=(unsigned char*)LIBRDF_MALLOC(cstring, length);
if(!buffer)
return NULL;
sprintf((char*)buffer, "r%dr%dr%d", id, pid, counter);
return buffer;
}
/* OLD INTERFACES BELOW HERE */
/* For old interfaces below ONLY */
#ifndef NO_STATIC_DATA
static librdf_world* RDF_World;
#endif
/**
* librdf_init_world:
* @digest_factory_name: Name of digest factory to use
* @not_used2: Not used
*
* Initialise the library
* @deprecated: Do not use.
*
* Use librdf_new_world() and librdf_world_open() on #librdf_world object
*
* See librdf_world_set_digest_factory_name() for documentation on arguments.
**/
void
librdf_init_world(char *digest_factory_name, void* not_used2)
{
#ifndef NO_STATIC_DATA
RDF_World=librdf_new_world();
if(!RDF_World)
return;
if(digest_factory_name)
librdf_world_set_digest(RDF_World, digest_factory_name);
librdf_world_open(RDF_World);
#else
/* fail if NO_STATIC_DATA is defined */
abort();
#endif
}
/**
* librdf_destroy_world:
*
* Terminate the library
* @deprecated: Do not use.
*
* Use librdf_free_world() on #librdf_world object
*
* Terminates and frees the resources.
**/
void
librdf_destroy_world(void)
{
#ifndef NO_STATIC_DATA
librdf_free_world(RDF_World);
#else
/* fail if NO_STATIC_DATA is defined */
abort();
#endif
}
/**
* librdf_basename:
* @name: path
*
* Get the basename of a path
*
* Return value: filename part of a pathname
**/
const char*
librdf_basename(const char *name)
{
char *p;
if((p=strrchr(name, '/')))
name=p+1;
else if((p=strrchr(name, '\\')))
name=p+1;
return name;
}
#if defined (LIBRDF_DEBUG) && defined(LIBRDF_MEMORY_SIGN)
void*
librdf_sign_malloc(size_t size)
{
int *p;
size += sizeof(int);
p=(int*)malloc(size);
*p++ = LIBRDF_SIGN_KEY;
return p;
}
void*
librdf_sign_calloc(size_t nmemb, size_t size)
{
int *p;
/* turn into bytes */
size = nmemb*size + sizeof(int);
p=(int*)calloc(1, size);
*p++ = LIBRDF_SIGN_KEY;
return p;
}
void*
librdf_sign_realloc(void *ptr, size_t size)
{
int *p;
if(!ptr)
return librdf_sign_malloc(size);
p=(int*)ptr;
p--;
if(*p != LIBRDF_SIGN_KEY)
LIBRDF_FATAL1(NULL, LIBRDF_FROM_MEMORY, "memory signature wrong\n");
size += sizeof(int);
p=(int*)realloc(p, size);
*p++= LIBRDF_SIGN_KEY;
return p;
}
void
librdf_sign_free(void *ptr)
{
int *p;
if(!ptr)
return;
p=(int*)ptr;
p--;
if(*p != LIBRDF_SIGN_KEY)
LIBRDF_FATAL1(NULL, LIBRDF_FROM_MEMORY, "memory signature wrong");
free(p);
}
#endif
#if defined (LIBRDF_DEBUG) && defined(HAVE_DMALLOC_H) && defined(LIBRDF_MEMORY_DEBUG_DMALLOC)
#undef malloc
void*
librdf_system_malloc(size_t size)
{
return malloc(size);
}
#undef free
void
librdf_system_free(void *ptr)
{
return free(ptr);
}
#endif
#endif
/* TEST CODE */
#ifdef STANDALONE
/* one more prototype */
int main(int argc, char *argv[]);
int
main(int argc, char *argv[])
{
librdf_world *world;
/* Minimal setup-cleanup test */
world=librdf_new_world();
librdf_free_world(world);
/* keep gcc -Wall happy */
return(0);
}
#endif
|