2 * Copyright 2013-2016 Nest Labs Inc. All Rights Reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 * This file defines C++ functions for safely performing simple,
20 * memory-mapped accesses, potentially to unaligned memory
27 #include <nlio-base.h>
36 * This determines whether the specified pointer is aligned on the
37 * indicated size boundary.
39 * @note The size should be a power of 2 (e.g. 1, 2, 4, 8, 16, etc.).
41 * @param[in] p The pointer to check the alignment of.
42 * @param[in] size The size, in bytes, boundary to check
43 * the alignment against.
45 * @return True if the pointer is aligned to the specified size;
48 static inline bool IsAligned(const void *p, size_t size)
50 return nlIOIsAligned(p, size);
54 * This determines whether the specified pointer is aligned on the
55 * size boundary for the template type, T.
57 * @tparam T The type of pointer to check the alignment of.
59 * @param[in] p The pointer to check the alignment of.
61 * @return True if the pointer is aligned to size of the type;
65 static inline bool IsAligned(const T *p)
67 return nlIOIsAligned(p, sizeof(T));
71 * Perform an aligned memory read of the 8-bit value at the specified
74 * @param[in] p A pointer address to read the 8-bit value from.
76 * @return The 8-bit value at the specified pointer address.
78 static inline uint8_t GetAligned8(const void *p)
80 return nlIOGetAligned8(p);
84 * Perform an aligned memory read of the 16-bit value at the specified
87 * @param[in] p A pointer address to read the 16-bit value from.
89 * @return The 16-bit value at the specified pointer address.
91 static inline uint16_t GetAligned16(const void *p)
93 return nlIOGetAligned16(p);
97 * Perform an aligned memory read of the 32-bit value at the specified
100 * @param[in] p A pointer address to read the 32-bit value from.
102 * @return The 32-bit value at the specified pointer address.
104 static inline uint32_t GetAligned32(const void *p)
106 return nlIOGetAligned32(p);
110 * Perform an aligned memory read of the 64-bit value at the specified
113 * @param[in] p A pointer address to read the 64-bit value from.
115 * @return The 64-bit value at the specified pointer address.
117 static inline uint64_t GetAligned64(const void *p)
119 return nlIOGetAligned64(p);
123 * Perform an unaligned memory read of the 8-bit value at the specified
126 * @param[in] p A pointer address to read the 8-bit value from.
128 * @return The 8-bit value at the specified pointer address.
130 static inline uint8_t GetUnaligned8(const void *p)
132 return nlIOGetUnaligned8(p);
136 * Perform an unaligned memory read of the 16-bit value at the specified
139 * @param[in] p A pointer address to read the 16-bit value from.
141 * @return The 16-bit value at the specified pointer address.
143 static inline uint16_t GetUnaligned16(const void *p)
145 return nlIOGetUnaligned16(p);
149 * Perform an unaligned memory read of the 32-bit value at the specified
152 * @param[in] p A pointer address to read the 32-bit value from.
154 * @return The 32-bit value at the specified pointer address.
156 static inline uint32_t GetUnaligned32(const void *p)
158 return nlIOGetUnaligned32(p);
162 * Perform an unaligned memory read of the 64-bit value at the specified
165 * @param[in] p A pointer address to read the 64-bit value from.
167 * @return The 64-bit value at the specified pointer address.
169 static inline uint64_t GetUnaligned64(const void *p)
171 return nlIOGetUnaligned64(p);
175 * Perform a, potentially unaligned, memory read of the 8-bit value
176 * from the specified pointer address.
178 * @param[in] p A pointer address, potentially unaligned, to read
179 * the 8-bit value from.
181 * @return The 8-bit value at the specified pointer address.
183 static inline uint8_t GetMaybeAligned8(const void *p)
185 return nlIOGetMaybeAligned8(p);
189 * Perform a, potentially unaligned, memory read of the 16-bit value
190 * from the specified pointer address.
192 * @param[in] p A pointer address, potentially unaligned, to read
193 * the 16-bit value from.
195 * @return The 16-bit value at the specified pointer address.
197 static inline uint16_t GetMaybeAligned16(const void *p)
199 return nlIOGetMaybeAligned16(p);
203 * Perform a, potentially unaligned, memory read of the 32-bit value
204 * from the specified pointer address.
206 * @param[in] p A pointer address, potentially unaligned, to read
207 * the 32-bit value from.
209 * @return The 32-bit value at the specified pointer address.
211 static inline uint32_t GetMaybeAligned32(const void *p)
213 return nlIOGetMaybeAligned32(p);
217 * Perform a, potentially unaligned, memory read of the 64-bit value
218 * from the specified pointer address.
220 * @param[in] p A pointer address, potentially unaligned, to read
221 * the 64-bit value from.
223 * @return The 64-bit value at the specified pointer address.
225 static inline uint64_t GetMaybeAligned64(const void *p)
227 return nlIOGetMaybeAligned64(p);
231 * Perform an aligned memory write of the 8-bit value to the specified
234 * @param[in] p A pointer address to write the 8-bit value to.
236 * @param[in] v The 8-bit value to write.
239 static inline void PutAligned8(void *p, const uint8_t v)
241 nlIOPutAligned8(p, v);
245 * Perform an aligned memory write of the 16-bit value to the specified
248 * @param[in] p A pointer address to write the 16-bit value to.
250 * @param[in] v The 16-bit value to write.
253 static inline void PutAligned16(void *p, const uint16_t v)
255 nlIOPutAligned16(p, v);
259 * Perform an aligned memory write of the 32-bit value to the specified
262 * @param[in] p A pointer address to write the 32-bit value to.
264 * @param[in] v The 32-bit value to write.
267 static inline void PutAligned32(void *p, const uint32_t v)
269 nlIOPutAligned32(p, v);
273 * Perform an aligned memory write of the 64-bit value to the specified
276 * @param[in] p A pointer address to write the 64-bit value to.
278 * @param[in] v The 64-bit value to write.
281 static inline void PutAligned64(void *p, const uint64_t v)
283 nlIOPutAligned64(p, v);
287 * Perform an unaligned memory write of the 8-bit value to the specified
290 * @param[in] p A pointer address to write the 8-bit value to.
292 * @param[in] v The 8-bit value to write.
295 static inline void PutUnaligned8(void *p, const uint8_t v)
297 nlIOPutUnaligned8(p, v);
301 * Perform an unaligned memory write of the 16-bit value to the specified
304 * @param[in] p A pointer address to write the 16-bit value to.
306 * @param[in] v The 16-bit value to write.
309 static inline void PutUnaligned16(void *p, const uint16_t v)
311 nlIOPutUnaligned16(p, v);
315 * Perform an unaligned memory write of the 32-bit value to the specified
318 * @param[in] p A pointer address to write the 32-bit value to.
320 * @param[in] v The 32-bit value to write.
323 static inline void PutUnaligned32(void *p, const uint32_t v)
325 nlIOPutUnaligned32(p, v);
329 * Perform an unaligned memory write of the 64-bit value to the specified
332 * @param[in] p A pointer address to write the 64-bit value to.
334 * @param[in] v The 64-bit value to write.
337 static inline void PutUnaligned64(void *p, const uint64_t v)
339 nlIOPutUnaligned64(p, v);
343 * Perform a, potentially unaligned, memory write of the 8-bit value
344 * to the specified pointer address.
346 * @param[in] p A pointer address, potentially unaligned, to write
347 * the 8-bit value to.
349 * @param[in] v The 8-bit value to write.
352 static inline void PutMaybeAligned8(void *p, const uint8_t v)
354 nlIOPutMaybeAligned8(p, v);
358 * Perform a, potentially unaligned, memory write of the 16-bit value
359 * to the specified pointer address.
361 * @param[in] p A pointer address, potentially unaligned, to write
362 * the 16-bit value to.
364 * @param[in] v The 16-bit value to write.
367 static inline void PutMaybeAligned16(void *p, const uint16_t v)
369 nlIOPutMaybeAligned16(p, v);
373 * Perform a, potentially unaligned, memory write of the 32-bit value
374 * to the specified pointer address.
376 * @param[in] p A pointer address, potentially unaligned, to write
377 * the 32-bit value to.
379 * @param[in] v The 32-bit value to write.
382 static inline void PutMaybeAligned32(void *p, const uint32_t v)
384 nlIOPutMaybeAligned32(p, v);
388 * Perform a, potentially unaligned, memory write of the 64-bit value
389 * to the specified pointer address.
391 * @param[in] p A pointer address, potentially unaligned, to write
392 * the 64-bit value to.
394 * @param[in] v The 64-bit value to write.
397 static inline void PutMaybeAligned64(void *p, const uint64_t v)
399 nlIOPutMaybeAligned64(p, v);
403 * Perform an aligned memory read of the 8-bit value at the specified
404 * pointer address and increment the pointer by 8-bits (1 byte).
406 * @param[inout] p A reference to a pointer address to read the
407 * 8-bit value from and to then increment by
410 * @return The 8-bit value at the specified pointer address.
412 static inline uint8_t ReadAligned8(const void *&p)
414 return nlIOReadAligned8(&p);
418 * Perform an aligned memory read of the 16-bit value at the specified
419 * pointer address and increment the pointer by 16-bits (2 bytes).
421 * @param[inout] p A reference to a pointer address to read the
422 * 16-bit value from and to then increment by
425 * @return The 16-bit value at the specified pointer address.
427 static inline uint16_t ReadAligned16(const void *&p)
429 return nlIOReadAligned16(&p);
433 * Perform an aligned memory read of the 32-bit value at the specified
434 * pointer address and increment the pointer by 32-bits (4 bytes).
436 * @param[inout] p A reference to a pointer address to read the
437 * 32-bit value from and to then increment by
440 * @return The 32-bit value at the specified pointer address.
442 static inline uint32_t ReadAligned32(const void *&p)
444 return nlIOReadAligned32(&p);
448 * Perform an aligned memory read of the 64-bit value at the specified
449 * pointer address and increment the pointer by 64-bits (8 bytes).
451 * @param[inout] p A reference to a pointer address to read the
452 * 64-bit value from and to then increment by
455 * @return The 64-bit value at the specified pointer address.
457 static inline uint64_t ReadAligned64(const void *&p)
459 return nlIOReadAligned64(&p);
463 * Perform an unaligned memory read of the 8-bit value at the specified
464 * pointer address and increment the pointer by 8-bits (1 byte).
466 * @param[inout] p A reference to a pointer address to read the
467 * 8-bit value from and to then increment by
470 * @return The 8-bit value at the specified pointer address.
472 static inline uint8_t ReadUnaligned8(const void *&p)
474 return nlIOReadUnaligned8(&p);
478 * Perform an unaligned memory read of the 16-bit value at the specified
479 * pointer address and increment the pointer by 16-bits (2 bytes).
481 * @param[inout] p A reference to a pointer address to read the
482 * 16-bit value from and to then increment by
485 * @return The 16-bit value at the specified pointer address.
487 static inline uint16_t ReadUnaligned16(const void *&p)
489 return nlIOReadUnaligned16(&p);
493 * Perform an aligned memory read of the 32-bit value at the specified
494 * pointer address and increment the pointer by 32-bits (4 bytes).
496 * @param[inout] p A reference to a pointer address to read the
497 * 32-bit value from and to then increment by
500 * @return The 32-bit value at the specified pointer address.
502 static inline uint32_t ReadUnaligned32(const void *&p)
504 return nlIOReadUnaligned32(&p);
508 * Perform an unaligned memory read of the 64-bit value at the specified
509 * pointer address and increment the pointer by 64-bits (8 bytes).
511 * @param[inout] p A reference to a pointer address to read the
512 * 64-bit value from and to then increment by
515 * @return The 64-bit value at the specified pointer address.
517 static inline uint64_t ReadUnaligned64(const void *&p)
519 return nlIOReadUnaligned64(&p);
523 * Perform a, potentially unaligned, memory read of the 8-bit value
524 * from the specified pointer address and increment the pointer by
527 * @param[inout] p A reference to a pointer address, potentially
528 * unaligned, to read the 8-bit value from and to
529 * then increment by 8-bits (1 byte).
531 * @return The 8-bit value at the specified pointer address.
533 static inline uint8_t ReadMaybeAligned8(const void *&p)
535 return nlIOReadMaybeAligned8(&p);
539 * Perform a, potentially unaligned, memory read of the 16-bit value
540 * from the specified pointer address and increment the pointer by
543 * @param[inout] p A reference to a pointer address, potentially
544 * unaligned, to read the 16-bit value from and to
545 * then increment by 16-bits (2 bytes).
547 * @return The 16-bit value at the specified pointer address.
549 static inline uint16_t ReadMaybeAligned16(const void *&p)
551 return nlIOReadMaybeAligned16(&p);
555 * Perform a, potentially unaligned, memory read of the 32-bit value
556 * from the specified pointer address and increment the pointer by
559 * @param[inout] p A reference to a pointer address, potentially
560 * unaligned, to read the 32-bit value from and to
561 * then increment by 32-bits (4 bytes).
563 * @return The 32-bit value at the specified pointer address.
565 static inline uint32_t ReadMaybeAligned32(const void *&p)
567 return nlIOReadMaybeAligned32(&p);
571 * Perform a, potentially unaligned, memory read of the 64-bit value
572 * from the specified pointer address and increment the pointer by
575 * @param[inout] p A reference to a pointer address, potentially
576 * unaligned, to read the 64-bit value from and to
577 * then increment by 64-bits (8 bytes).
579 * @return The 64-bit value at the specified pointer address.
581 static inline uint64_t ReadMaybeAligned64(const void *&p)
583 return nlIOReadMaybeAligned64(&p);
587 * Perform an aligned memory write of the 8-bit value to the specified
588 * pointer address and increment the pointer by 8-bits (1 byte).
590 * @param[inout] p A reference to a pointer address to read the 8-bit
591 * value from and to then increment by 8-bits (1 byte).
593 * @param[in] v The 8-bit value to write.
596 static inline void WriteAligned8(void *&p, const uint8_t v)
598 nlIOWriteAligned8(&p, v);
602 * Perform an aligned memory write of the 16-bit value to the specified
603 * pointer address and increment the pointer by 16-bits (2 bytes).
605 * @param[inout] p A reference to a pointer address to read the 16-bit
606 * value from and to then increment by 16-bits (2 bytes).
608 * @param[in] v The 16-bit value to write.
611 static inline void WriteAligned16(void *&p, const uint16_t v)
613 nlIOWriteAligned16(&p, v);
617 * Perform an aligned memory write of the 32-bit value to the specified
618 * pointer address and increment the pointer by 32-bits (4 bytes).
620 * @param[inout] p A reference to a pointer address to read the 32-bit
621 * value from and to then increment by 32-bits (4 bytes).
623 * @param[in] v The 32-bit value to write.
626 static inline void WriteAligned32(void *&p, const uint32_t v)
628 nlIOWriteAligned32(&p, v);
632 * Perform an aligned memory write of the 64-bit value to the specified
633 * pointer address and increment the pointer by 64-bits (8 bytes).
635 * @param[inout] p A reference to a pointer address to read the 64-bit
636 * value from and to then increment by 64-bits (8 bytes).
638 * @param[in] v The 64-bit value to write.
641 static inline void WriteAligned64(void *&p, const uint64_t v)
643 nlIOWriteAligned64(&p, v);
647 * Perform an unaligned memory write of the 8-bit value to the specified
648 * pointer address and increment the pointer by 8-bits (1 byte).
650 * @param[inout] p A reference to a pointer address to read the 8-bit
651 * value from and to then increment by 8-bits (1 byte).
653 * @param[in] v The 8-bit value to write.
656 static inline void WriteUnaligned8(void *&p, const uint8_t v)
658 nlIOWriteUnaligned8(&p, v);
662 * Perform an unaligned memory write of the 16-bit value to the specified
663 * pointer address and increment the pointer by 16-bits (2 bytes).
665 * @param[inout] p A reference to a pointer address to read the 16-bit
666 * value from and to then increment by 16-bits (2 bytes).
668 * @param[in] v The 16-bit value to write.
671 static inline void WriteUnaligned16(void *&p, const uint16_t v)
673 nlIOWriteUnaligned16(&p, v);
677 * Perform an unaligned memory write of the 32-bit value to the specified
678 * pointer address and increment the pointer by 32-bits (4 bytes).
680 * @param[inout] p A reference to a pointer address to read the 32-bit
681 * value from and to then increment by 32-bits (4 bytes).
683 * @param[in] v The 32-bit value to write.
686 static inline void WriteUnaligned32(void *&p, const uint32_t v)
688 nlIOWriteUnaligned32(&p, v);
692 * Perform an unaligned memory write of the 64-bit value to the specified
693 * pointer address and increment the pointer by 64-bits (8 bytes).
695 * @param[inout] p A reference to a pointer address to read the 64-bit
696 * value from and to then increment by 64-bits (8 bytes).
698 * @param[in] v The 64-bit value to write.
701 static inline void WriteUnaligned64(void *&p, const uint64_t v)
703 nlIOWriteUnaligned64(&p, v);
707 * Perform a, potentially unaligned, memory write of the 8-bit value
708 * to the specified pointer address and increment the pointer by
711 * @param[inout] p A reference to a pointer address, potentially
712 * unaligned, to read the 8-bit value from and to
713 * then increment by 8-bits (1 byte).
715 * @param[in] v The 8-bit value to write.
718 static inline void WriteMaybeAligned8(void *&p, const uint8_t v)
720 nlIOWriteMaybeAligned8(&p, v);
724 * Perform a, potentially unaligned, memory write of the 16-bit value
725 * to the specified pointer address and increment the pointer by
728 * @param[inout] p A reference to a pointer address, potentially
729 * unaligned, to read the 16-bit value from and to
730 * then increment by 16-bits (2 bytes).
732 * @param[in] v The 16-bit value to write.
735 static inline void WriteMaybeAligned16(void *&p, const uint16_t v)
737 nlIOWriteMaybeAligned16(&p, v);
741 * Perform a, potentially unaligned, memory write of the 32-bit value
742 * to the specified pointer address and increment the pointer by
745 * @param[inout] p A reference to a pointer address, potentially
746 * unaligned, to read the 32-bit value from and to
747 * then increment by 32-bits (4 bytes).
749 * @param[in] v The 32-bit value to write.
752 static inline void WriteMaybeAligned32(void *&p, const uint32_t v)
754 nlIOWriteMaybeAligned32(&p, v);
758 * Perform a, potentially unaligned, memory write of the 64-bit value
759 * to the specified pointer address and increment the pointer by
762 * @param[inout] p A reference to a pointer address, potentially
763 * unaligned, to read the 64-bit value from and to
764 * then increment by 64-bits (8 bytes).
766 * @param[in] v The 64-bit value to write.
769 static inline void WriteMaybeAligned64(void *&p, const uint64_t v)
771 nlIOWriteMaybeAligned64(&p, v);
775 * Perform a memory read of the 8-bit value at the specified pointer
778 * @param[in] p A pointer address to read the 8-bit value from.
780 * @return The 8-bit value at the specified pointer address.
782 static inline uint8_t Get8(const void *p)
788 * Perform a, potentially unaligned, memory read of the 16-bit value
789 * from the specified pointer address.
791 * @param[in] p A pointer address, potentially unaligned, to read
792 * the 16-bit value from.
794 * @return The 16-bit value at the specified pointer address.
796 static inline uint16_t Get16(const void *p)
802 * Perform a, potentially unaligned, memory read of the 32-bit value
803 * from the specified pointer address.
805 * @param[in] p A pointer address, potentially unaligned, to read
806 * the 32-bit value from.
808 * @return The 32-bit value at the specified pointer address.
810 static inline uint32_t Get32(const void *p)
816 * Perform a, potentially unaligned, memory read of the 64-bit value
817 * from the specified pointer address.
819 * @param[in] p A pointer address, potentially unaligned, to read
820 * the 64-bit value from.
822 * @return The 64-bit value at the specified pointer address.
824 static inline uint64_t Get64(const void *p)
830 * Perform a, potentially unaligned, memory write of the 8-bit value
831 * to the specified pointer address.
833 * @param[in] p A pointer address, potentially unaligned, to write
834 * the 8-bit value to.
836 * @param[in] v The 8-bit value to write.
839 static inline void Put8(void *p, const uint8_t v)
845 * Perform a, potentially unaligned, memory write of the 16-bit value
846 * to the specified pointer address.
848 * @param[in] p A pointer address, potentially unaligned, to write
849 * the 16-bit value to.
851 * @param[in] v The 16-bit value to write.
854 static inline void Put16(void *p, const uint16_t v)
860 * Perform a, potentially unaligned, memory write of the 32-bit value
861 * to the specified pointer address.
863 * @param[in] p A pointer address, potentially unaligned, to write
864 * the 32-bit value to.
866 * @param[in] v The 32-bit value to write.
869 static inline void Put32(void *p, const uint32_t v)
875 * Perform a, potentially unaligned, memory write of the 64-bit value
876 * to the specified pointer address.
878 * @param[in] p A pointer address, potentially unaligned, to write
879 * the 64-bit value to.
881 * @param[in] v The 64-bit value to write.
884 static inline void Put64(void *p, const uint64_t v)
890 * Perform a, potentially unaligned, memory read of the 16-bit value
891 * from the specified pointer address and increment the pointer by
894 * @param[inout] p A reference to a pointer address, potentially
895 * unaligned, to read the 8-bit value from and to
896 * then increment by 8-bits (1 byte).
898 * @return The 8-bit value at the specified pointer address.
900 static inline uint8_t Read8(const void *&p)
902 return nlIORead8(&p);
906 * Perform a, potentially unaligned, memory read of the 16-bit value
907 * from the specified pointer address and increment the pointer by
910 * @param[inout] p A reference to a pointer address, potentially
911 * unaligned, to read the 16-bit value from and to
912 * then increment by 16-bits (2 bytes).
914 * @return The 16-bit value at the specified pointer address.
916 static inline uint16_t Read16(const void *&p)
918 return nlIORead16(&p);
922 * Perform a, potentially unaligned, memory read of the 32-bit value
923 * from the specified pointer address and increment the pointer by
926 * @param[inout] p A reference to a pointer address, potentially
927 * unaligned, to read the 32-bit value from and to
928 * then increment by 32-bits (4 bytes).
930 * @return The 32-bit value at the specified pointer address.
932 static inline uint32_t Read32(const void *&p)
934 return nlIORead32(&p);
938 * Perform a, potentially unaligned, memory read of the 64-bit value
939 * from the specified pointer address and increment the pointer by
942 * @param[inout] p A reference to a pointer address, potentially
943 * unaligned, to read the 64-bit value from and to
944 * then increment by 64-bits (8 bytes).
946 * @return The 64-bit value at the specified pointer address.
948 static inline uint64_t Read64(const void *&p)
950 return nlIORead64(&p);
954 * Perform a, potentially unaligned, memory write of the 8-bit value
955 * to the specified pointer address and increment the pointer by
958 * @param[inout] p A reference to a pointer address, potentially
959 * unaligned, to read the 8-bit value from and to
960 * then increment by 8-bits (1 byte).
962 * @param[in] v The 8-bit value to write.
965 static inline void Write8(void *&p, const uint8_t v)
971 * Perform a, potentially unaligned, memory write of the 16-bit value
972 * to the specified pointer address and increment the pointer by
975 * @param[inout] p A reference to a pointer address, potentially
976 * unaligned, to read the 16-bit value from and to
977 * then increment by 16-bits (2 bytes).
979 * @param[in] v The 16-bit value to write.
982 static inline void Write16(void *&p, const uint16_t v)
988 * Perform a, potentially unaligned, memory write of the 32-bit value
989 * to the specified pointer address and increment the pointer by
992 * @param[inout] p A reference to a pointer address, potentially
993 * unaligned, to read the 32-bit value from and to
994 * then increment by 32-bits (4 bytes).
996 * @param[in] v The 32-bit value to write.
999 static inline void Write32(void *&p, const uint32_t v)
1005 * Perform a, potentially unaligned, memory write of the 64-bit value
1006 * to the specified pointer address and increment the pointer by
1007 * 64-bits (8 bytes).
1009 * @param[inout] p A reference to a pointer address, potentially
1010 * unaligned, to read the 64-bit value from and to
1011 * then increment by 64-bits (8 bytes).
1013 * @param[in] v The 64-bit value to write.
1016 static inline void Write64(void *&p, const uint64_t v)
1025 #endif // NLIO_BASE_HPP