1 |
/* |
2 |
* Copyright (c) 2003 Luke Macpherson. |
3 |
* All rights reserved. |
4 |
* |
5 |
* Redistribution and use in source and binary forms, with or without modification, |
6 |
* are permitted provided that the following conditions are met: |
7 |
* |
8 |
* 1. Redistributions of source code must retain the above copyright notice, |
9 |
* this list of conditions and the following disclaimer. |
10 |
* 2. Redistributions in binary form must reproduce the above copyright notice, |
11 |
* this list of conditions and the following disclaimer in the documentation |
12 |
* and/or other materials provided with the distribution. |
13 |
* 3. The name of the author may not be used to endorse or promote products |
14 |
* derived from this software without specific prior written permission. |
15 |
* |
16 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
17 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
18 |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
19 |
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
20 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
21 |
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
22 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
24 |
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
25 |
* OF SUCH DAMAGE. |
26 |
* |
27 |
* This file is part of the lwIP TCP/IP stack. |
28 |
* |
29 |
* Author: Luke Macpherson <lukem@cse.unsw.edu.au> |
30 |
* |
31 |
*/ |
32 |
|
33 |
#include "lwip/pbuf.h" |
34 |
#include "lwip/pbuf-ram.h" |
35 |
#include "lwip/mem.h" |
36 |
|
37 |
struct pbuf_manager pbuf_ram_manager = {pbuf_ram_alloc, |
38 |
pbuf_ram_realloc, |
39 |
pbuf_ram_free, |
40 |
pbuf_ram_header}; |
41 |
|
42 |
void |
43 |
pbuf_ram_init(void){ |
44 |
return; |
45 |
} |
46 |
|
47 |
struct pbuf * |
48 |
pbuf_ram_alloc(u16_t offset, u16_t size, struct pbuf_manager *mgr, void *src){ |
49 |
struct pbuf_ram *p; |
50 |
|
51 |
p = mem_malloc(MEM_ALIGN_SIZE(sizeof(struct pbuf_ram) + offset + size)); |
52 |
if (p == NULL) { |
53 |
return NULL; |
54 |
} |
55 |
|
56 |
p->next = NULL; |
57 |
p->payload = MEM_ALIGN((void*)((u8_t*)p + sizeof(struct pbuf) + offset)); |
58 |
p->manager = mgr; |
59 |
p->tot_len = size; |
60 |
p->len = size; |
61 |
p->ref = 1; |
62 |
#ifdef LWIP_DEBUG |
63 |
p->magic = PBUF_RAM_MAGIC; |
64 |
#endif |
65 |
|
66 |
/* if source data is specified, copy it to the payload */ |
67 |
if(src!=NULL && size>0){ |
68 |
memcpy(p->payload, src, size); |
69 |
} |
70 |
|
71 |
return (struct pbuf*)p; |
72 |
} |
73 |
|
74 |
void |
75 |
pbuf_ram_realloc(struct pbuf *p, u16_t size){ |
76 |
mem_realloc(p, ((u8_t*)p->payload - (u8_t*)p) + size); |
77 |
return; |
78 |
} |
79 |
|
80 |
void |
81 |
pbuf_ram_free(struct pbuf *p){ |
82 |
#ifdef LWIP_DEBUG |
83 |
struct pbuf_ram *q = (struct pbuf_ram*)p; |
84 |
LWIP_ASSERT("pbuf_ram_free(): pbuf was not a ram pbuf!", |
85 |
(q->magic==PBUF_RAM_MAGIC)); |
86 |
#endif |
87 |
mem_free(p); |
88 |
} |
89 |
|
90 |
u8_t |
91 |
pbuf_ram_header(struct pbuf *p, s16_t header_size){ |
92 |
mem_ptr_t min, payload; |
93 |
|
94 |
LWIP_DEBUGF(PBUF_DEBUG, ("pbuf_ram_header(0x%p,%d) was called.\n", |
95 |
p, (int)header_size)); |
96 |
|
97 |
min = (mem_ptr_t)p + sizeof(struct pbuf_ram); |
98 |
payload = (mem_ptr_t)p->payload; |
99 |
|
100 |
if(payload - header_size >= min){ |
101 |
p->payload = (u8_t*)p->payload - header_size; |
102 |
p->len += header_size; |
103 |
p->tot_len += header_size; |
104 |
return 0; /* success */ |
105 |
} |
106 |
|
107 |
return 1; /* failure */ |
108 |
} |