Import proplib-0.6.1.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
/* $NetBSD: prop_number.c,v 1.20 2008/11/30 00:17:07 haad Exp $ */
|
||||
/* $NetBSD: prop_number.c,v 1.23 2010/09/24 22:51:52 rmind Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2006 The NetBSD Foundation, Inc.
|
||||
@@ -33,8 +33,16 @@
|
||||
#include "prop_object_impl.h"
|
||||
#include "prop_rb_impl.h"
|
||||
|
||||
#if defined(_KERNEL)
|
||||
#include <sys/systm.h>
|
||||
#elif defined(_STANDALONE)
|
||||
#include <sys/param.h>
|
||||
#include <lib/libkern/libkern.h>
|
||||
#else
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#define __unused /* empty */
|
||||
#endif
|
||||
|
||||
struct _prop_number {
|
||||
struct _prop_object pn_obj;
|
||||
@@ -51,10 +59,6 @@ struct _prop_number {
|
||||
} pn_value;
|
||||
};
|
||||
|
||||
#define RBNODE_TO_PN(n) \
|
||||
((struct _prop_number *) \
|
||||
((uintptr_t)n - offsetof(struct _prop_number, pn_link)))
|
||||
|
||||
_PROP_POOL_INIT(_prop_number_pool, sizeof(struct _prop_number), "propnmbr")
|
||||
|
||||
static _prop_object_free_rv_t
|
||||
@@ -115,33 +119,34 @@ _prop_number_compare_values(const struct _prop_number_value *pnv1,
|
||||
}
|
||||
|
||||
static int
|
||||
_prop_number_rb_compare_nodes(const struct rb_node *n1,
|
||||
const struct rb_node *n2)
|
||||
/*ARGSUSED*/
|
||||
_prop_number_rb_compare_nodes(void *ctx __unused,
|
||||
const void *n1, const void *n2)
|
||||
{
|
||||
const prop_number_t pn1 = RBNODE_TO_PN(n1);
|
||||
const prop_number_t pn2 = RBNODE_TO_PN(n2);
|
||||
const struct _prop_number *pn1 = n1;
|
||||
const struct _prop_number *pn2 = n2;
|
||||
|
||||
return (_prop_number_compare_values(&pn1->pn_value, &pn2->pn_value));
|
||||
return _prop_number_compare_values(&pn1->pn_value, &pn2->pn_value);
|
||||
}
|
||||
|
||||
static int
|
||||
_prop_number_rb_compare_key(const struct rb_node *n,
|
||||
const void *v)
|
||||
/*ARGSUSED*/
|
||||
_prop_number_rb_compare_key(void *ctx __unused, const void *n, const void *v)
|
||||
{
|
||||
const prop_number_t pn = RBNODE_TO_PN(n);
|
||||
const struct _prop_number *pn = n;
|
||||
const struct _prop_number_value *pnv = v;
|
||||
|
||||
return (_prop_number_compare_values(&pn->pn_value, pnv));
|
||||
return _prop_number_compare_values(&pn->pn_value, pnv);
|
||||
}
|
||||
|
||||
static const struct rb_tree_ops _prop_number_rb_tree_ops = {
|
||||
static const rb_tree_ops_t _prop_number_rb_tree_ops = {
|
||||
.rbto_compare_nodes = _prop_number_rb_compare_nodes,
|
||||
.rbto_compare_key = _prop_number_rb_compare_key,
|
||||
.rbto_compare_key = _prop_number_rb_compare_key,
|
||||
.rbto_node_offset = offsetof(struct _prop_number, pn_link),
|
||||
.rbto_context = NULL
|
||||
};
|
||||
|
||||
static struct rb_tree _prop_number_tree;
|
||||
static bool _prop_number_tree_initialized;
|
||||
|
||||
_PROP_MUTEX_DECL_STATIC(_prop_number_tree_mutex)
|
||||
|
||||
/* ARGSUSED */
|
||||
@@ -150,21 +155,34 @@ _prop_number_free(prop_stack_t stack, prop_object_t *obj)
|
||||
{
|
||||
prop_number_t pn = *obj;
|
||||
|
||||
_prop_rb_tree_remove_node(&_prop_number_tree, &pn->pn_link);
|
||||
_prop_rb_tree_remove_node(&_prop_number_tree, pn);
|
||||
|
||||
_PROP_POOL_PUT(_prop_number_pool, pn);
|
||||
|
||||
return (_PROP_OBJECT_FREE_DONE);
|
||||
}
|
||||
|
||||
static void
|
||||
_prop_number_lock()
|
||||
_PROP_ONCE_DECL(_prop_number_init_once)
|
||||
|
||||
static int
|
||||
_prop_number_init(void)
|
||||
{
|
||||
|
||||
_PROP_MUTEX_INIT(_prop_number_tree_mutex);
|
||||
_prop_rb_tree_init(&_prop_number_tree, &_prop_number_rb_tree_ops);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
_prop_number_lock(void)
|
||||
{
|
||||
/* XXX: init necessary? */
|
||||
_PROP_ONCE_RUN(_prop_number_init_once, _prop_number_init);
|
||||
_PROP_MUTEX_LOCK(_prop_number_tree_mutex);
|
||||
}
|
||||
|
||||
static void
|
||||
_prop_number_unlock()
|
||||
_prop_number_unlock(void)
|
||||
{
|
||||
_PROP_MUTEX_UNLOCK(_prop_number_tree_mutex);
|
||||
}
|
||||
@@ -178,8 +196,9 @@ _prop_number_externalize(struct _prop_object_externalize_context *ctx,
|
||||
|
||||
/*
|
||||
* For the record:
|
||||
* The original implementation used hex for signed numbers,
|
||||
* but we changed it to be human readable.
|
||||
* the original NetBSD implementation used hexadecimal for unsigned
|
||||
* numbers, but in the portable proplib we changed it to be human
|
||||
* readable (base 10).
|
||||
*/
|
||||
if (pn->pn_value.pnv_is_unsigned)
|
||||
sprintf(tmpstr, "%" PRIu64, pn->pn_value.pnv_unsigned);
|
||||
@@ -253,27 +272,20 @@ _prop_number_equals(prop_object_t v1, prop_object_t v2,
|
||||
static prop_number_t
|
||||
_prop_number_alloc(const struct _prop_number_value *pnv)
|
||||
{
|
||||
prop_number_t opn, pn;
|
||||
struct rb_node *n;
|
||||
bool rv;
|
||||
prop_number_t opn, pn, rpn;
|
||||
|
||||
_PROP_ONCE_RUN(_prop_number_init_once, _prop_number_init);
|
||||
|
||||
/*
|
||||
* Check to see if this already exists in the tree. If it does,
|
||||
* we just retain it and return it.
|
||||
*/
|
||||
_PROP_MUTEX_LOCK(_prop_number_tree_mutex);
|
||||
if (! _prop_number_tree_initialized) {
|
||||
_prop_rb_tree_init(&_prop_number_tree,
|
||||
&_prop_number_rb_tree_ops);
|
||||
_prop_number_tree_initialized = true;
|
||||
} else {
|
||||
n = _prop_rb_tree_find(&_prop_number_tree, pnv);
|
||||
if (n != NULL) {
|
||||
opn = RBNODE_TO_PN(n);
|
||||
prop_object_retain(opn);
|
||||
_PROP_MUTEX_UNLOCK(_prop_number_tree_mutex);
|
||||
return (opn);
|
||||
}
|
||||
opn = _prop_rb_tree_find(&_prop_number_tree, pnv);
|
||||
if (opn != NULL) {
|
||||
prop_object_retain(opn);
|
||||
_PROP_MUTEX_UNLOCK(_prop_number_tree_mutex);
|
||||
return (opn);
|
||||
}
|
||||
_PROP_MUTEX_UNLOCK(_prop_number_tree_mutex);
|
||||
|
||||
@@ -294,16 +306,15 @@ _prop_number_alloc(const struct _prop_number_value *pnv)
|
||||
* we have to check again if it is in the tree.
|
||||
*/
|
||||
_PROP_MUTEX_LOCK(_prop_number_tree_mutex);
|
||||
n = _prop_rb_tree_find(&_prop_number_tree, pnv);
|
||||
if (n != NULL) {
|
||||
opn = RBNODE_TO_PN(n);
|
||||
opn = _prop_rb_tree_find(&_prop_number_tree, pnv);
|
||||
if (opn != NULL) {
|
||||
prop_object_retain(opn);
|
||||
_PROP_MUTEX_UNLOCK(_prop_number_tree_mutex);
|
||||
_PROP_POOL_PUT(_prop_number_pool, pn);
|
||||
return (opn);
|
||||
}
|
||||
rv = _prop_rb_tree_insert_node(&_prop_number_tree, &pn->pn_link);
|
||||
_PROP_ASSERT(rv == true);
|
||||
rpn = _prop_rb_tree_insert_node(&_prop_number_tree, pn);
|
||||
_PROP_ASSERT(rpn == pn);
|
||||
_PROP_MUTEX_UNLOCK(_prop_number_tree_mutex);
|
||||
return (pn);
|
||||
}
|
||||
@@ -501,10 +512,14 @@ _prop_number_internalize_unsigned(struct _prop_object_internalize_context *ctx,
|
||||
_PROP_ASSERT(/*CONSTCOND*/sizeof(unsigned long long) ==
|
||||
sizeof(uint64_t));
|
||||
|
||||
#ifndef _KERNEL
|
||||
errno = 0;
|
||||
#endif
|
||||
pnv->pnv_unsigned = (uint64_t) strtoull(ctx->poic_cp, &cp, 0);
|
||||
#ifndef _KERNEL /* XXX can't check for ERANGE in the kernel */
|
||||
if (pnv->pnv_unsigned == UINT64_MAX && errno == ERANGE)
|
||||
return (false);
|
||||
#endif
|
||||
pnv->pnv_is_unsigned = true;
|
||||
ctx->poic_cp = cp;
|
||||
|
||||
@@ -519,11 +534,15 @@ _prop_number_internalize_signed(struct _prop_object_internalize_context *ctx,
|
||||
|
||||
_PROP_ASSERT(/*CONSTCOND*/sizeof(long long) == sizeof(int64_t));
|
||||
|
||||
#ifndef _KERNEL
|
||||
errno = 0;
|
||||
#endif
|
||||
pnv->pnv_signed = (int64_t) strtoll(ctx->poic_cp, &cp, 0);
|
||||
#ifndef _KERNEL /* XXX can't check for ERANGE in the kernel */
|
||||
if ((pnv->pnv_signed == INT64_MAX || pnv->pnv_signed == INT64_MIN) &&
|
||||
errno == ERANGE)
|
||||
return (false);
|
||||
#endif
|
||||
pnv->pnv_is_unsigned = false;
|
||||
ctx->poic_cp = cp;
|
||||
|
||||
|
Reference in New Issue
Block a user