Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
697
698
699
700
/*	$NetBSD: prop_string.c,v 1.17 2022/08/03 21:13:46 riastradh Exp $	*/

/*-
 * Copyright (c) 2006, 2020 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Jason R. Thorpe.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "prop_object_impl.h"
#include <prop/prop_string.h>

#include <sys/rbtree.h>
#if defined(_KERNEL) || defined(_STANDALONE)
#include <sys/stdarg.h>
#else
#include <stdarg.h>
#endif /* _KERNEL || _STANDALONE */

struct _prop_string {
	struct _prop_object	ps_obj;
	union {
		char *		psu_mutable;
		const char *	psu_immutable;
	} ps_un;
#define	ps_mutable		ps_un.psu_mutable
#define	ps_immutable		ps_un.psu_immutable
	size_t			ps_size;	/* not including \0 */
	struct rb_node		ps_link;
	int			ps_flags;
};

#define	PS_F_NOCOPY		0x01
#define	PS_F_MUTABLE		0x02

_PROP_POOL_INIT(_prop_string_pool, sizeof(struct _prop_string), "propstng")

_PROP_MALLOC_DEFINE(M_PROP_STRING, "prop string",
		    "property string container object")

static _prop_object_free_rv_t
		_prop_string_free(prop_stack_t, prop_object_t *);
static bool	_prop_string_externalize(
				struct _prop_object_externalize_context *,
				void *);
static _prop_object_equals_rv_t
		_prop_string_equals(prop_object_t, prop_object_t,
				    void **, void **,
				    prop_object_t *, prop_object_t *);

static const struct _prop_object_type _prop_object_type_string = {
	.pot_type	=	PROP_TYPE_STRING,
	.pot_free	=	_prop_string_free,
	.pot_extern	=	_prop_string_externalize,
	.pot_equals	=	_prop_string_equals,
};

#define	prop_object_is_string(x)	\
	((x) != NULL && (x)->ps_obj.po_type == &_prop_object_type_string)
#define	prop_string_contents(x)  ((x)->ps_immutable ? (x)->ps_immutable : "")

/*
 * In order to reduce memory usage, all immutable string objects are
 * de-duplicated.
 */

static int
/*ARGSUSED*/
_prop_string_rb_compare_nodes(void *ctx _PROP_ARG_UNUSED,
			      const void *n1, const void *n2)
{
	const struct _prop_string * const ps1 = n1;
	const struct _prop_string * const ps2 = n2;

	_PROP_ASSERT(ps1->ps_immutable != NULL);
	_PROP_ASSERT(ps2->ps_immutable != NULL);

	return strcmp(ps1->ps_immutable, ps2->ps_immutable);
}

static int
/*ARGSUSED*/
_prop_string_rb_compare_key(void *ctx _PROP_ARG_UNUSED,
			    const void *n, const void *v)
{
	const struct _prop_string * const ps = n;
	const char * const cp = v;

	_PROP_ASSERT(ps->ps_immutable != NULL);

	return strcmp(ps->ps_immutable, cp);
}

static const rb_tree_ops_t _prop_string_rb_tree_ops = {
	.rbto_compare_nodes = _prop_string_rb_compare_nodes,
	.rbto_compare_key = _prop_string_rb_compare_key,
	.rbto_node_offset = offsetof(struct _prop_string, ps_link),
	.rbto_context = NULL
};

static struct rb_tree _prop_string_tree;

_PROP_ONCE_DECL(_prop_string_init_once)
_PROP_MUTEX_DECL_STATIC(_prop_string_tree_mutex)

static int
_prop_string_init(void)
{

	_PROP_MUTEX_INIT(_prop_string_tree_mutex);
	rb_tree_init(&_prop_string_tree,
		     &_prop_string_rb_tree_ops);

	return 0;
}

/* ARGSUSED */
static _prop_object_free_rv_t
_prop_string_free(prop_stack_t stack, prop_object_t *obj)
{
	prop_string_t ps = *obj;

	if ((ps->ps_flags & PS_F_MUTABLE) == 0) {
		_PROP_MUTEX_LOCK(_prop_string_tree_mutex);
		/*
		 * Double-check the retain count now that we've
		 * acquired the tree lock; holding this lock prevents
		 * new retains from coming in by finding it in the
		 * tree.
		 */
		if (_PROP_ATOMIC_LOAD(&ps->ps_obj.po_refcnt) == 0)
			rb_tree_remove_node(&_prop_string_tree, ps);
		else
			ps = NULL;
		_PROP_MUTEX_UNLOCK(_prop_string_tree_mutex);

		if (ps == NULL)
			return (_PROP_OBJECT_FREE_DONE);
	}

	if ((ps->ps_flags & PS_F_NOCOPY) == 0 && ps->ps_mutable != NULL)
	    	_PROP_FREE(ps->ps_mutable, M_PROP_STRING);
	_PROP_POOL_PUT(_prop_string_pool, ps);

	return (_PROP_OBJECT_FREE_DONE);
}

static bool
_prop_string_externalize(struct _prop_object_externalize_context *ctx,
			 void *v)
{
	prop_string_t ps = v;

	if (ps->ps_size == 0)
		return (_prop_object_externalize_empty_tag(ctx, "string"));

	if (_prop_object_externalize_start_tag(ctx, "string") == false ||
	    _prop_object_externalize_append_encoded_cstring(ctx,
	    					ps->ps_immutable) == false ||
	    _prop_object_externalize_end_tag(ctx, "string") == false)
		return (false);

	return (true);
}

/* ARGSUSED */
static _prop_object_equals_rv_t
_prop_string_equals(prop_object_t v1, prop_object_t v2,
    void **stored_pointer1, void **stored_pointer2,
    prop_object_t *next_obj1, prop_object_t *next_obj2)
{
	prop_string_t str1 = v1;
	prop_string_t str2 = v2;

	if (str1 == str2)
		return (_PROP_OBJECT_EQUALS_TRUE);
	if (str1->ps_size != str2->ps_size)
		return (_PROP_OBJECT_EQUALS_FALSE);
	if (strcmp(prop_string_contents(str1), prop_string_contents(str2)))
		return (_PROP_OBJECT_EQUALS_FALSE);
	else
		return (_PROP_OBJECT_EQUALS_TRUE);
}

static prop_string_t
_prop_string_alloc(int const flags)
{
	prop_string_t ps;

	ps = _PROP_POOL_GET(_prop_string_pool);
	if (ps != NULL) {
		_prop_object_init(&ps->ps_obj, &_prop_object_type_string);

		ps->ps_mutable = NULL;
		ps->ps_size = 0;
		ps->ps_flags = flags;
	}

	return (ps);
}

static prop_string_t
_prop_string_instantiate(int const flags, const char * const str,
    size_t const len)
{
	prop_string_t ps;

	_PROP_ONCE_RUN(_prop_string_init_once, _prop_string_init);

	ps = _prop_string_alloc(flags);
	if (ps != NULL) {
		ps->ps_immutable = str;
		ps->ps_size = len;

		if ((flags & PS_F_MUTABLE) == 0) {
			prop_string_t ops;

			_PROP_MUTEX_LOCK(_prop_string_tree_mutex);
			ops = rb_tree_insert_node(&_prop_string_tree, ps);
			if (ops != ps) {
				/*
				 * Equivalent string object already exist;
				 * free the new one and return a reference
				 * to the existing object.
				 */
				prop_object_retain(ops);
				_PROP_MUTEX_UNLOCK(_prop_string_tree_mutex);
				_PROP_POOL_PUT(_prop_string_pool, ps);
				ps = ops;
			} else {
				_PROP_MUTEX_UNLOCK(_prop_string_tree_mutex);
			}
		}
	}

	return (ps);
}

_PROP_DEPRECATED(prop_string_create,
    "this program uses prop_string_create(); all functions "
    "supporting mutable prop_strings are deprecated.")
prop_string_t
prop_string_create(void)
{

	return (_prop_string_alloc(PS_F_MUTABLE));
}

_PROP_DEPRECATED(prop_string_create_cstring,
    "this program uses prop_string_create_cstring(); all functions "
    "supporting mutable prop_strings are deprecated.")
prop_string_t
prop_string_create_cstring(const char *str)
{
	prop_string_t ps;
	char *cp;
	size_t len;

	_PROP_ASSERT(str != NULL);

	ps = _prop_string_alloc(PS_F_MUTABLE);
	if (ps != NULL) {
		len = strlen(str);
		cp = _PROP_MALLOC(len + 1, M_PROP_STRING);
		if (cp == NULL) {
			prop_object_release(ps);
			return (NULL);
		}
		strcpy(cp, str);
		ps->ps_mutable = cp;
		ps->ps_size = len;
	}
	return (ps);
}

_PROP_DEPRECATED(prop_string_create_cstring_nocopy,
    "this program uses prop_string_create_cstring_nocopy(), "
    "which is deprecated; use prop_string_create_nocopy() instead.")
prop_string_t
prop_string_create_cstring_nocopy(const char *str)
{
	return prop_string_create_nocopy(str);
}

/*
 * prop_string_create_format --
 *	Create a string object using the provided format string.
 */
prop_string_t __printflike(1, 2)
prop_string_create_format(const char *fmt, ...)
{
	prop_string_t ps;
	char *str = NULL;
	int len;
	size_t nlen;
	va_list ap;

	_PROP_ASSERT(fmt != NULL);

	va_start(ap, fmt);
	len = vsnprintf(NULL, 0, fmt, ap);
	va_end(ap);

	if (len < 0)
		return (NULL);
	nlen = len + 1;

	str = _PROP_MALLOC(nlen, M_PROP_STRING);
	if (str == NULL)
		return (NULL);

	va_start(ap, fmt);
	vsnprintf(str, nlen, fmt, ap);
	va_end(ap);

	ps = _prop_string_instantiate(0, str, (size_t)len);
	if (ps == NULL)
		_PROP_FREE(str, M_PROP_STRING);

	return (ps);
}

/*
 * prop_string_create_copy --
 *	Create a string object by coping the provided constant string.
 */
prop_string_t
prop_string_create_copy(const char *str)
{
	return prop_string_create_format("%s", str);
}

/*
 * prop_string_create_nocopy --
 *	Create a string object using the provided external constant
 *	string.
 */
prop_string_t
prop_string_create_nocopy(const char *str)
{

	_PROP_ASSERT(str != NULL);

	return _prop_string_instantiate(PS_F_NOCOPY, str, strlen(str));
}

/*
 * prop_string_copy --
 *	Copy a string.  This reduces to a retain in the common case.
 *	Deprecated mutable string objects must be copied.
 */
prop_string_t
prop_string_copy(prop_string_t ops)
{
	prop_string_t ps;
	char *cp;

	if (! prop_object_is_string(ops))
		return (NULL);

	if ((ops->ps_flags & PS_F_MUTABLE) == 0) {
		prop_object_retain(ops);
		return (ops);
	}

	cp = _PROP_MALLOC(ops->ps_size + 1, M_PROP_STRING);
	if (cp == NULL)
		return NULL;

	strcpy(cp, prop_string_contents(ops));

	ps = _prop_string_instantiate(PS_F_MUTABLE, cp, ops->ps_size);
	if (ps == NULL)
		_PROP_FREE(cp, M_PROP_STRING);

	return (ps);
}

_PROP_DEPRECATED(prop_string_copy_mutable,
    "this program uses prop_string_copy_mutable(); all functions "
    "supporting mutable prop_strings are deprecated.")
prop_string_t
prop_string_copy_mutable(prop_string_t ops)
{
	prop_string_t ps;
	char *cp;

	if (! prop_object_is_string(ops))
		return (NULL);

	cp = _PROP_MALLOC(ops->ps_size + 1, M_PROP_STRING);
	if (cp == NULL)
		return NULL;

	strcpy(cp, prop_string_contents(ops));

	ps = _prop_string_instantiate(PS_F_MUTABLE, cp, ops->ps_size);
	if (ps == NULL)
		_PROP_FREE(cp, M_PROP_STRING);

	return (ps);
}

/*
 * prop_string_size --
 *	Return the size of the string, not including the terminating NUL.
 */
size_t
prop_string_size(prop_string_t ps)
{

	if (! prop_object_is_string(ps))
		return (0);

	return (ps->ps_size);
}

/*
 * prop_string_value --
 *	Returns a pointer to the string object's value.  This pointer
 *	remains valid only as long as the string object.
 */
const char *
prop_string_value(prop_string_t ps)
{

	if (! prop_object_is_string(ps))
		return (NULL);

	if ((ps->ps_flags & PS_F_MUTABLE) == 0)
		return (ps->ps_immutable);

	return (prop_string_contents(ps));
}

/*
 * prop_string_copy_value --
 *	Copy the string object's value into the supplied buffer.
 */
bool
prop_string_copy_value(prop_string_t ps, void *buf, size_t buflen)
{

	if (! prop_object_is_string(ps))
		return (false);

	if (buf == NULL || buflen < ps->ps_size + 1)
		return (false);

	strcpy(buf, prop_string_contents(ps));

	return (true);
}

_PROP_DEPRECATED(prop_string_mutable,
    "this program uses prop_string_mutable(); all functions "
    "supporting mutable prop_strings are deprecated.")
bool
prop_string_mutable(prop_string_t ps)
{

	if (! prop_object_is_string(ps))
		return (false);

	return ((ps->ps_flags & PS_F_MUTABLE) != 0);
}

_PROP_DEPRECATED(prop_string_cstring,
    "this program uses prop_string_cstring(), "
    "which is deprecated; use prop_string_copy_value() instead.")
char *
prop_string_cstring(prop_string_t ps)
{
	char *cp;

	if (! prop_object_is_string(ps))
		return (NULL);

	cp = _PROP_MALLOC(ps->ps_size + 1, M_TEMP);
	if (cp != NULL)
		strcpy(cp, prop_string_contents(ps));

	return (cp);
}

_PROP_DEPRECATED(prop_string_cstring_nocopy,
    "this program uses prop_string_cstring_nocopy(), "
    "which is deprecated; use prop_string_value() instead.")
const char *
prop_string_cstring_nocopy(prop_string_t ps)
{

	if (! prop_object_is_string(ps))
		return (NULL);

	return (prop_string_contents(ps));
}

_PROP_DEPRECATED(prop_string_append,
    "this program uses prop_string_append(); all functions "
    "supporting mutable prop_strings are deprecated.")
bool
prop_string_append(prop_string_t dst, prop_string_t src)
{
	char *ocp, *cp;
	size_t len;

	if (! (prop_object_is_string(dst) &&
	       prop_object_is_string(src)))
		return (false);

	if ((dst->ps_flags & PS_F_MUTABLE) == 0)
		return (false);

	len = dst->ps_size + src->ps_size;
	cp = _PROP_MALLOC(len + 1, M_PROP_STRING);
	if (cp == NULL)
		return (false);
	snprintf(cp, len + 1, "%s%s", prop_string_contents(dst),
		prop_string_contents(src));
	ocp = dst->ps_mutable;
	dst->ps_mutable = cp;
	dst->ps_size = len;
	if (ocp != NULL)
		_PROP_FREE(ocp, M_PROP_STRING);

	return (true);
}

_PROP_DEPRECATED(prop_string_append_cstring,
    "this program uses prop_string_append_cstring(); all functions "
    "supporting mutable prop_strings are deprecated.")
bool
prop_string_append_cstring(prop_string_t dst, const char *src)
{
	char *ocp, *cp;
	size_t len;

	if (! prop_object_is_string(dst))
		return (false);

	_PROP_ASSERT(src != NULL);

	if ((dst->ps_flags & PS_F_MUTABLE) == 0)
		return (false);

	len = dst->ps_size + strlen(src);
	cp = _PROP_MALLOC(len + 1, M_PROP_STRING);
	if (cp == NULL)
		return (false);
	snprintf(cp, len + 1, "%s%s", prop_string_contents(dst), src);
	ocp = dst->ps_mutable;
	dst->ps_mutable = cp;
	dst->ps_size = len;
	if (ocp != NULL)
		_PROP_FREE(ocp, M_PROP_STRING);

	return (true);
}

/*
 * prop_string_equals --
 *	Return true if two strings are equivalent.
 */
bool
prop_string_equals(prop_string_t str1, prop_string_t str2)
{
	if (!prop_object_is_string(str1) || !prop_object_is_string(str2))
		return (false);

	return prop_object_equals(str1, str2);
}

/*
 * prop_string_equals_string --
 *	Return true if the string object is equivalent to the specified
 *	C string.
 */
bool
prop_string_equals_string(prop_string_t ps, const char *cp)
{

	if (! prop_object_is_string(ps))
		return (false);

	return (strcmp(prop_string_contents(ps), cp) == 0);
}

_PROP_DEPRECATED(prop_string_equals_cstring,
    "this program uses prop_string_equals_cstring(), "
    "which is deprecated; prop_string_equals_string() instead.")
bool
prop_string_equals_cstring(prop_string_t ps, const char *cp)
{
	return prop_string_equals_string(ps, cp);
}

/*
 * prop_string_compare --
 *	Compare two string objects, using strcmp() semantics.
 */
int
prop_string_compare(prop_string_t ps1, prop_string_t ps2)
{
	if (!prop_object_is_string(ps1) || !prop_object_is_string(ps2))
		return (-666);	/* arbitrary */

	return (strcmp(prop_string_contents(ps1),
		       prop_string_contents(ps2)));
}

/*
 * prop_string_compare_string --
 *	Compare a string object to the specified C string, using
 *	strcmp() semantics.
 */
int
prop_string_compare_string(prop_string_t ps, const char *cp)
{
	if (!prop_object_is_string(ps))
		return (-666);	/* arbitrary */

	return (strcmp(prop_string_contents(ps), cp));
}

/*
 * _prop_string_internalize --
 *	Parse a <string>...</string> and return the object created from the
 *	external representation.
 */
/* ARGSUSED */
bool
_prop_string_internalize(prop_stack_t stack, prop_object_t *obj,
    struct _prop_object_internalize_context *ctx)
{
	prop_string_t string;
	char *str;
	size_t len, alen;

	if (ctx->poic_is_empty_element) {
		*obj = prop_string_create();
		return (true);
	}

	/* No attributes recognized here. */
	if (ctx->poic_tagattr != NULL)
		return (true);

	/* Compute the length of the result. */
	if (_prop_object_internalize_decode_string(ctx, NULL, 0, &len,
						   NULL) == false)
		return (true);

	str = _PROP_MALLOC(len + 1, M_PROP_STRING);
	if (str == NULL)
		return (true);

	if (_prop_object_internalize_decode_string(ctx, str, len, &alen,
						   &ctx->poic_cp) == false ||
	    alen != len) {
		_PROP_FREE(str, M_PROP_STRING);
		return (true);
	}
	str[len] = '\0';

	if (_prop_object_internalize_find_tag(ctx, "string",
					      _PROP_TAG_TYPE_END) == false) {
		_PROP_FREE(str, M_PROP_STRING);
		return (true);
	}

	string = _prop_string_instantiate(0, str, len);
	if (string == NULL)
		_PROP_FREE(str, M_PROP_STRING);

	*obj = string;
	return (true);
}