xref: /trafficserver/include/tscore/ink_align.h (revision 079a4027)
1 /** @file
2 
3   A brief file description
4 
5   @section license License
6 
7   Licensed to the Apache Software Foundation (ASF) under one
8   or more contributor license agreements.  See the NOTICE file
9   distributed with this work for additional information
10   regarding copyright ownership.  The ASF licenses this file
11   to you under the Apache License, Version 2.0 (the
12   "License"); you may not use this file except in compliance
13   with the License.  You may obtain a copy of the License at
14 
15       http://www.apache.org/licenses/LICENSE-2.0
16 
17   Unless required by applicable law or agreed to in writing, software
18   distributed under the License is distributed on an "AS IS" BASIS,
19   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   See the License for the specific language governing permissions and
21   limitations under the License.
22  */
23 
24 //-*-c++-*-
25 #pragma once
26 
27 #include "tscore/ink_time.h"
28 
29 union Alias32 {
30   uint8_t byte[4];
31   int32_t i32;
32   uint32_t u32;
33 };
34 
35 union Alias64 {
36   uint8_t byte[8];
37   int32_t i32[2];
38   uint32_t u32[2];
39   int64_t i64;
40   uint64_t u64;
41   ink_time_t i_time;
42 };
43 
44 /**
45  * Alignment macros
46  */
47 
48 #define INK_MIN_ALIGN 8
49 /* INK_ALIGN() is only to be used to align on a power of 2 boundary */
50 #define INK_ALIGN(size, boundary) (((size) + ((boundary)-1)) & ~((boundary)-1))
51 
52 /** Default alignment */
53 #define INK_ALIGN_DEFAULT(size) INK_ALIGN(size, INK_MIN_ALIGN)
54 
55 //
56 // Move a pointer forward until it meets the alignment width.
57 //
58 static inline void *
align_pointer_forward(const void * pointer_,size_t alignment)59 align_pointer_forward(const void *pointer_, size_t alignment)
60 {
61   char *pointer = (char *)pointer_;
62   //
63   // Round up alignment..
64   //
65   pointer = (char *)INK_ALIGN((ptrdiff_t)pointer, alignment);
66 
67   return (void *)pointer;
68 }
69 
70 //
71 // Move a pointer forward until it meets the alignment width specified,
72 // and zero out the contents of the space you're skipping over.
73 //
74 static inline void *
align_pointer_forward_and_zero(const void * pointer_,size_t alignment)75 align_pointer_forward_and_zero(const void *pointer_, size_t alignment)
76 {
77   char *pointer = (char *)pointer_;
78   char *aligned = (char *)INK_ALIGN((ptrdiff_t)pointer, alignment);
79   //
80   // Fill the skippings..
81   //
82   while (pointer < aligned) {
83     *pointer = 0;
84     pointer++;
85   }
86 
87   return (void *)aligned;
88 }
89 
90 //
91 // We include two signatures for the same function to avoid error
92 // messages concerning coercion between void* and unsigned long.
93 // We could handle this using casts, but that's more prone to
94 // errors during porting.
95 //
96