libcoap  4.2.0
str.c
Go to the documentation of this file.
1 /* str.c -- strings to be used in the CoAP library
2  *
3  * Copyright (C) 2010,2011 Olaf Bergmann <bergmann@tzi.org>
4  *
5  * This file is part of the CoAP library libcoap. Please see
6  * README for terms of use.
7  */
8 
9 #include "coap_config.h"
10 
11 #include <stdio.h>
12 
13 #include "libcoap.h"
14 #include "coap_debug.h"
15 #include "mem.h"
16 #include "str.h"
17 
19  coap_string_t *s =
21  if ( !s ) {
22 #ifndef NDEBUG
23  coap_log(LOG_CRIT, "coap_new_string: malloc\n");
24 #endif
25  return NULL;
26  }
27 
28  memset(s, 0, sizeof(coap_string_t));
29  s->s = ((unsigned char *)s) + sizeof(coap_string_t);
30  s->s[size] = '\000';
31  return s;
32 }
33 
36 }
37 
38 coap_str_const_t *coap_new_str_const(const uint8_t *data, size_t size) {
39  coap_string_t *s = coap_new_string(size);
40  if (!s)
41  return NULL;
42  memcpy (s->s, data, size);
43  s->length = size;
44  return (coap_str_const_t *)s;
45 }
46 
49 }
50 
uint8_t * s
string data
Definition: str.h:27
Coap string data definition.
Definition: str.h:25
Coap string data definition with const data.
Definition: str.h:33
coap_string_t * coap_new_string(size_t size)
Returns a new string object with at least size+1 bytes storage allocated.
Definition: str.c:18
void coap_delete_string(coap_string_t *s)
Deletes the given string and releases any memory allocated.
Definition: str.c:34
void coap_delete_str_const(coap_str_const_t *s)
Deletes the given const string and releases any memory allocated.
Definition: str.c:47
struct coap_string_t coap_string_t
Coap string data definition.
size_t length
length of string
Definition: str.h:26
void * coap_malloc_type(coap_memory_tag_t type, size_t size)
Allocates a chunk of size bytes and returns a pointer to the newly allocated memory.
#define coap_log(level,...)
Logging function.
Definition: coap_debug.h:122
coap_str_const_t * coap_new_str_const(const uint8_t *data, size_t size)
Returns a new const string object with at least size+1 bytes storage allocated, and the provided data...
Definition: str.c:38
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
unsigned char uint8_t
Definition: uthash.h:79
Critical.
Definition: coap_debug.h:44