2017-06-24 00:53:59 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
|
2017-08-10 01:52:19 +02:00
|
|
|
#include <err.h>
|
2017-08-09 03:29:51 +02:00
|
|
|
#include <math.h>
|
2017-06-24 00:53:59 +02:00
|
|
|
#include <sched.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2017-08-09 03:29:51 +02:00
|
|
|
#include <wchar.h>
|
2017-07-25 05:01:13 +02:00
|
|
|
|
2017-06-24 00:53:59 +02:00
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
// This function returns a pointer less than 2 * alignment + or_mask bytes into the array.
|
2017-07-25 05:01:13 +02:00
|
|
|
char* GetAlignedMemory(char* orig_ptr, size_t alignment, size_t or_mask) {
|
2017-06-24 00:53:59 +02:00
|
|
|
if ((alignment & (alignment - 1)) != 0) {
|
2017-08-10 01:52:19 +02:00
|
|
|
errx(1, "warning: alignment passed into GetAlignedMemory is not a power of two.");
|
2017-06-24 00:53:59 +02:00
|
|
|
}
|
|
|
|
if (or_mask > alignment) {
|
2017-08-10 01:52:19 +02:00
|
|
|
errx(1, "warning: or_mask passed into GetAlignedMemory is too high.");
|
2017-06-24 00:53:59 +02:00
|
|
|
}
|
|
|
|
uintptr_t ptr = reinterpret_cast<uintptr_t>(orig_ptr);
|
|
|
|
if (alignment > 0) {
|
|
|
|
// When setting the alignment, set it to exactly the alignment chosen.
|
|
|
|
// The pointer returned will be guaranteed not to be aligned to anything
|
|
|
|
// more than that.
|
|
|
|
ptr += alignment - (ptr & (alignment - 1));
|
|
|
|
ptr |= alignment | or_mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
return reinterpret_cast<char*>(ptr);
|
|
|
|
}
|
|
|
|
|
2017-07-25 05:01:13 +02:00
|
|
|
char* GetAlignedPtr(std::vector<char>* buf, size_t alignment, size_t nbytes) {
|
2017-06-24 00:53:59 +02:00
|
|
|
buf->resize(nbytes + 3 * alignment);
|
|
|
|
return GetAlignedMemory(buf->data(), alignment, 0);
|
|
|
|
}
|
|
|
|
|
2017-08-09 03:29:51 +02:00
|
|
|
wchar_t* GetAlignedPtr(std::vector<wchar_t>* buf, size_t alignment, size_t nchars) {
|
|
|
|
buf->resize(nchars + ceil((3 * alignment) / sizeof(wchar_t)));
|
|
|
|
return reinterpret_cast<wchar_t*>(GetAlignedMemory(reinterpret_cast<char*>(buf->data()),
|
|
|
|
alignment, 0));
|
|
|
|
}
|
|
|
|
|
2017-07-25 05:01:13 +02:00
|
|
|
char* GetAlignedPtrFilled(std::vector<char>* buf, size_t alignment, size_t nbytes, char fill_byte) {
|
2017-06-24 00:53:59 +02:00
|
|
|
char* buf_aligned = GetAlignedPtr(buf, alignment, nbytes);
|
|
|
|
memset(buf_aligned, fill_byte, nbytes);
|
|
|
|
return buf_aligned;
|
|
|
|
}
|
|
|
|
|
2017-07-13 08:10:07 +02:00
|
|
|
#if defined(__APPLE__)
|
2017-07-13 04:33:32 +02:00
|
|
|
|
|
|
|
// Darwin doesn't support this, so do nothing.
|
|
|
|
bool LockToCPU(int) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2018-05-04 22:27:47 +02:00
|
|
|
bool LockToCPU(int cpu_to_lock) {
|
2017-06-24 00:53:59 +02:00
|
|
|
cpu_set_t cpuset;
|
|
|
|
|
|
|
|
CPU_ZERO(&cpuset);
|
|
|
|
CPU_SET(cpu_to_lock, &cpuset);
|
|
|
|
if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0) {
|
2018-05-04 22:27:47 +02:00
|
|
|
if (errno == EINVAL) {
|
|
|
|
printf("Invalid cpu %d\n", cpu_to_lock);
|
|
|
|
} else {
|
|
|
|
perror("sched_setaffinity failed");
|
|
|
|
}
|
2017-06-24 00:53:59 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2017-07-13 04:33:32 +02:00
|
|
|
|
|
|
|
#endif
|