blob: ec74147cdd1d211a5edf5933be3cf13d26f8ca25 [file] [log] [blame]
Anders Lewisf4447b92017-06-23 15:53:59 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#include "util.h"
18
Anders Lewisa98a5fb2017-08-09 16:52:19 -070019#include <err.h>
Anders Lewisac4f4b42017-08-08 18:29:51 -070020#include <math.h>
Anders Lewisf4447b92017-06-23 15:53:59 -070021#include <sched.h>
22#include <stdio.h>
23#include <string.h>
Anders Lewisac4f4b42017-08-08 18:29:51 -070024#include <wchar.h>
Anders Lewisa7b0f882017-07-24 20:01:13 -070025
Anders Lewisf4447b92017-06-23 15:53:59 -070026#include <cstdlib>
Anders Lewisf4447b92017-06-23 15:53:59 -070027
28// This function returns a pointer less than 2 * alignment + or_mask bytes into the array.
Anders Lewisa7b0f882017-07-24 20:01:13 -070029char* GetAlignedMemory(char* orig_ptr, size_t alignment, size_t or_mask) {
Anders Lewisf4447b92017-06-23 15:53:59 -070030 if ((alignment & (alignment - 1)) != 0) {
Anders Lewisa98a5fb2017-08-09 16:52:19 -070031 errx(1, "warning: alignment passed into GetAlignedMemory is not a power of two.");
Anders Lewisf4447b92017-06-23 15:53:59 -070032 }
33 if (or_mask > alignment) {
Anders Lewisa98a5fb2017-08-09 16:52:19 -070034 errx(1, "warning: or_mask passed into GetAlignedMemory is too high.");
Anders Lewisf4447b92017-06-23 15:53:59 -070035 }
36 uintptr_t ptr = reinterpret_cast<uintptr_t>(orig_ptr);
37 if (alignment > 0) {
38 // When setting the alignment, set it to exactly the alignment chosen.
39 // The pointer returned will be guaranteed not to be aligned to anything
40 // more than that.
41 ptr += alignment - (ptr & (alignment - 1));
42 ptr |= alignment | or_mask;
43 }
44
45 return reinterpret_cast<char*>(ptr);
46}
47
Anders Lewisa7b0f882017-07-24 20:01:13 -070048char* GetAlignedPtr(std::vector<char>* buf, size_t alignment, size_t nbytes) {
Anders Lewisf4447b92017-06-23 15:53:59 -070049 buf->resize(nbytes + 3 * alignment);
50 return GetAlignedMemory(buf->data(), alignment, 0);
51}
52
Anders Lewisac4f4b42017-08-08 18:29:51 -070053wchar_t* GetAlignedPtr(std::vector<wchar_t>* buf, size_t alignment, size_t nchars) {
54 buf->resize(nchars + ceil((3 * alignment) / sizeof(wchar_t)));
55 return reinterpret_cast<wchar_t*>(GetAlignedMemory(reinterpret_cast<char*>(buf->data()),
56 alignment, 0));
57}
58
Anders Lewisa7b0f882017-07-24 20:01:13 -070059char* GetAlignedPtrFilled(std::vector<char>* buf, size_t alignment, size_t nbytes, char fill_byte) {
Anders Lewisf4447b92017-06-23 15:53:59 -070060 char* buf_aligned = GetAlignedPtr(buf, alignment, nbytes);
61 memset(buf_aligned, fill_byte, nbytes);
62 return buf_aligned;
63}
64
Christopher Ferris5fc1f6e2017-07-12 23:10:07 -070065#if defined(__APPLE__)
Christopher Ferrisbad43c72017-07-12 19:33:32 -070066
67// Darwin doesn't support this, so do nothing.
68bool LockToCPU(int) {
69 return false;
70}
71
72#else
73
Christopher Ferrisbfb7c762018-05-04 13:27:47 -070074bool LockToCPU(int cpu_to_lock) {
Anders Lewisf4447b92017-06-23 15:53:59 -070075 cpu_set_t cpuset;
76
77 CPU_ZERO(&cpuset);
Anders Lewisf4447b92017-06-23 15:53:59 -070078 CPU_SET(cpu_to_lock, &cpuset);
79 if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0) {
Christopher Ferrisbfb7c762018-05-04 13:27:47 -070080 if (errno == EINVAL) {
81 printf("Invalid cpu %d\n", cpu_to_lock);
82 } else {
83 perror("sched_setaffinity failed");
84 }
Anders Lewisf4447b92017-06-23 15:53:59 -070085 return false;
86 }
87
88 return true;
89}
Christopher Ferrisbad43c72017-07-12 19:33:32 -070090
91#endif