nxt_string.c (10:a8e68ed06863) nxt_string.c (65:10688b89aa16)
1
2/*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) NGINX, Inc.
5 */
6
7#include <nxt_main.h>
8
9
10nxt_str_t *
1
2/*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) NGINX, Inc.
5 */
6
7#include <nxt_main.h>
8
9
10nxt_str_t *
11nxt_str_alloc(nxt_mem_pool_t *mp, size_t length)
11nxt_str_alloc(nxt_mp_t *mp, size_t length)
12{
13 nxt_str_t *s;
14
15 /* The string start is allocated aligned to be close to nxt_str_t. */
12{
13 nxt_str_t *s;
14
15 /* The string start is allocated aligned to be close to nxt_str_t. */
16 s = nxt_mem_alloc(mp, sizeof(nxt_str_t) + length);
16 s = nxt_mp_get(mp, sizeof(nxt_str_t) + length);
17
18 if (nxt_fast_path(s != NULL)) {
19 s->length = length;
20 s->start = (u_char *) s + sizeof(nxt_str_t);
21 }
22
23 return s;
24}
25
26
27/*
28 * nxt_str_dup() creates a new string with a copy of a source string.
29 * If length of the source string is zero, then the new string anyway
30 * gets a pointer somewhere in mem_pool.
31 */
32
33nxt_str_t *
17
18 if (nxt_fast_path(s != NULL)) {
19 s->length = length;
20 s->start = (u_char *) s + sizeof(nxt_str_t);
21 }
22
23 return s;
24}
25
26
27/*
28 * nxt_str_dup() creates a new string with a copy of a source string.
29 * If length of the source string is zero, then the new string anyway
30 * gets a pointer somewhere in mem_pool.
31 */
32
33nxt_str_t *
34nxt_str_dup(nxt_mem_pool_t *mp, nxt_str_t *dst, const nxt_str_t *src)
34nxt_str_dup(nxt_mp_t *mp, nxt_str_t *dst, const nxt_str_t *src)
35{
36 u_char *p;
37
38 if (dst == NULL) {
39 /* The string start is allocated aligned to be close to nxt_str_t. */
35{
36 u_char *p;
37
38 if (dst == NULL) {
39 /* The string start is allocated aligned to be close to nxt_str_t. */
40 dst = nxt_mem_alloc(mp, sizeof(nxt_str_t) + src->length);
40 dst = nxt_mp_get(mp, sizeof(nxt_str_t) + src->length);
41 if (nxt_slow_path(dst == NULL)) {
42 return NULL;
43 }
44
45 p = (u_char *) dst;
46 p += sizeof(nxt_str_t);
47 dst->start = p;
48
49 } else {
41 if (nxt_slow_path(dst == NULL)) {
42 return NULL;
43 }
44
45 p = (u_char *) dst;
46 p += sizeof(nxt_str_t);
47 dst->start = p;
48
49 } else {
50 dst->start = nxt_mem_nalloc(mp, src->length);
50 dst->start = nxt_mp_nget(mp, src->length);
51 if (nxt_slow_path(dst->start == NULL)) {
52 return NULL;
53 }
54 }
55
56 nxt_memcpy(dst->start, src->start, src->length);
57 dst->length = src->length;
58

--- 5 unchanged lines hidden (view full) ---

64 * nxt_str_copy() creates a C style zero-terminated copy of a source
65 * nxt_str_t. The function is intended to create strings suitable
66 * for libc and kernel interfaces so result is pointer to char instead
67 * of u_char to minimize casts. The copy is aligned to 2 bytes thus
68 * the lowest bit may be used as marker.
69 */
70
71char *
51 if (nxt_slow_path(dst->start == NULL)) {
52 return NULL;
53 }
54 }
55
56 nxt_memcpy(dst->start, src->start, src->length);
57 dst->length = src->length;
58

--- 5 unchanged lines hidden (view full) ---

64 * nxt_str_copy() creates a C style zero-terminated copy of a source
65 * nxt_str_t. The function is intended to create strings suitable
66 * for libc and kernel interfaces so result is pointer to char instead
67 * of u_char to minimize casts. The copy is aligned to 2 bytes thus
68 * the lowest bit may be used as marker.
69 */
70
71char *
72nxt_str_copy(nxt_mem_pool_t *mp, const nxt_str_t *src)
72nxt_str_copy(nxt_mp_t *mp, const nxt_str_t *src)
73{
74 char *p, *dst;
75
73{
74 char *p, *dst;
75
76 dst = nxt_mem_align(mp, 2, src->length + 1);
76 dst = nxt_mp_align(mp, 2, src->length + 1);
77
78 if (nxt_fast_path(dst != NULL)) {
79 p = nxt_cpymem(dst, src->start, src->length);
80 *p = '\0';
81 }
82
83 return dst;
84}

--- 234 unchanged lines hidden ---
77
78 if (nxt_fast_path(dst != NULL)) {
79 p = nxt_cpymem(dst, src->start, src->length);
80 *p = '\0';
81 }
82
83 return dst;
84}

--- 234 unchanged lines hidden ---