• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* libs/corecg/SkBuffer.cpp
2 **
3 ** Copyright 2006, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 **     http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17 
18 #include "SkBuffer.h"
19 
20 ////////////////////////////////////////////////////////////////////////////////////////
21 
readNoSizeCheck(void * buffer,size_t size)22 void SkRBuffer::readNoSizeCheck(void* buffer, size_t size)
23 {
24     SkASSERT((fData != 0 && fStop == 0) || fPos + size <= fStop);
25     if (buffer)
26         memcpy(buffer, fPos, size);
27     fPos += size;
28 }
29 
skip(size_t size)30 const void* SkRBuffer::skip(size_t size)
31 {
32     const void* result = fPos;
33     readNoSizeCheck(NULL, size);
34     return result;
35 }
36 
skipToAlign4()37 size_t SkRBuffer::skipToAlign4()
38 {
39     size_t pos = this->pos();
40     size_t n = SkAlign4(pos) - pos;
41     fPos += n;
42     return n;
43 }
44 
skip(size_t size)45 void* SkWBuffer::skip(size_t size)
46 {
47     void* result = fPos;
48     writeNoSizeCheck(NULL, size);
49     return fData == NULL ? NULL : result;
50 }
51 
writeNoSizeCheck(const void * buffer,size_t size)52 void SkWBuffer::writeNoSizeCheck(const void* buffer, size_t size)
53 {
54     SkASSERT(fData == 0 || fStop == 0 || fPos + size <= fStop);
55     if (fData && buffer)
56         memcpy(fPos, buffer, size);
57     fPos += size;
58 }
59 
padToAlign4()60 size_t SkWBuffer::padToAlign4()
61 {
62     size_t pos = this->pos();
63     size_t n = SkAlign4(pos) - pos;
64 
65     if (n && fData)
66     {
67         char* p = fPos;
68         char* stop = p + n;
69         do {
70             *p++ = 0;
71         } while (p < stop);
72     }
73     fPos += n;
74     return n;
75 }
76 
77 #if 0
78 #ifdef SK_DEBUG
79     static void AssertBuffer32(const void* buffer)
80     {
81         SkASSERT(buffer);
82         SkASSERT(((size_t)buffer & 3) == 0);
83     }
84 #else
85     #define AssertBuffer32(buffer)
86 #endif
87 
88 void* sk_buffer_write_int32(void* buffer, int32_t value)
89 {
90     AssertBuffer32(buffer);
91     *(int32_t*)buffer = value;
92     return (char*)buffer + sizeof(int32_t);
93 }
94 
95 void* sk_buffer_write_int32(void* buffer, const int32_t values[], int count)
96 {
97     AssertBuffer32(buffer);
98     SkASSERT(count >= 0);
99 
100     memcpy((int32_t*)buffer, values, count * sizeof(int32_t));
101     return (char*)buffer + count * sizeof(int32_t);
102 }
103 
104 const void* sk_buffer_read_int32(const void* buffer, int32_t* value)
105 {
106     AssertBuffer32(buffer);
107     if (value)
108         *value = *(const int32_t*)buffer;
109     return (const char*)buffer + sizeof(int32_t);
110 }
111 
112 const void* sk_buffer_read_int32(const void* buffer, int32_t values[], int count)
113 {
114     AssertBuffer32(buffer);
115     SkASSERT(count >= 0);
116 
117     if (values)
118         memcpy(values, (const int32_t*)buffer, count * sizeof(int32_t));
119     return (const char*)buffer + count * sizeof(int32_t);
120 }
121 
122 void* sk_buffer_write_ptr(void* buffer, void* ptr)
123 {
124     AssertBuffer32(buffer);
125     *(void**)buffer = ptr;
126     return (char*)buffer + sizeof(void*);
127 }
128 
129 const void* sk_buffer_read_ptr(const void* buffer, void** ptr)
130 {
131     AssertBuffer32(buffer);
132     if (ptr)
133         *ptr = *(void**)buffer;
134     return (const char*)buffer + sizeof(void*);
135 }
136 
137 #endif
138