Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/tscore/List.h
Original file line number Diff line number Diff line change
Expand Up @@ -721,12 +721,12 @@ template <class C, class L = typename C::Link_link> struct AtomicSLL {
C *
head()
{
return (C *)TO_PTR(FREELIST_POINTER(al.head));
return (C *)(al.head.load());
}
C *
next(C *c)
{
return (C *)TO_PTR(c);
return (C *)ink_atomiclist_next(&al, c);
}

InkAtomicList al;
Expand Down
143 changes: 9 additions & 134 deletions include/tscore/ink_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,141 +33,20 @@

***********************************************************************/

#include "tscore/ink_platform.h"
#include "tscore/ink_defs.h"
#include "tscore/ink_apidefs.h"

/*
For information on the structure of the x86_64 memory map:

http://en.wikipedia.org/wiki/X86-64#Linux

Essentially, in the current 48-bit implementations, the
top bit as well as the lower 47 bits are used, leaving
the upper-but one 16 bits free to be used for the version.
We will use the top-but-one 15 and sign extend when generating
the pointer was required by the standard.
*/
#include <atomic>
#include <cstdio>

/*
#if defined(POSIX_THREAD)
#include <pthread.h>
#include <stdlib.h>
#endif
*/
#include "tscore/ink_apidefs.h"

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

void ink_queue_load_64(void *dst, void *src);

#ifdef __x86_64__
#define INK_QUEUE_LD64(dst, src) *((uint64_t *)&(dst)) = *((uint64_t *)&(src))
#else
#define INK_QUEUE_LD64(dst, src) (ink_queue_load_64((void *)&(dst), (void *)&(src)))
#endif

#if TS_HAS_128BIT_CAS
#define INK_QUEUE_LD(dst, src) \
do { \
*(__int128_t *)&(dst) = __sync_val_compare_and_swap((__int128_t *)&(src), 0, 0); \
} while (0)
#else
#define INK_QUEUE_LD(dst, src) INK_QUEUE_LD64(dst, src)
#endif

/*
* Generic Free List Manager
*/
// Warning: head_p is read and written in multiple threads without a
// lock, use INK_QUEUE_LD to read safely.
union head_p {
head_p() : data(){};

#if (defined(__i386__) || defined(__arm__) || defined(__mips__)) && (SIZEOF_VOIDP == 4)
typedef int32_t version_type;
typedef int64_t data_type;
#elif TS_HAS_128BIT_CAS
typedef int64_t version_type;
typedef __int128_t data_type;
#else
typedef int64_t version_type;
typedef int64_t data_type;
#endif

struct {
void *pointer;
version_type version;
} s;

data_type data;
};

/*
* Why is version required? One scenario is described below
* Think of a list like this -> A -> C -> D
* and you are popping from the list
* Between the time you take the ptr(A) and swap the head pointer
* the list could start looking like this
* -> A -> B -> C -> D
* If the version check is not there, the list will look like
* -> C -> D after the pop, which will result in the loss of "B"
*/
#define ZERO_HEAD_P(_x)

#ifdef DEBUG
#define FROM_PTR(_x) (void *)(((uintptr_t)_x) + 1)
#define TO_PTR(_x) (void *)(((uintptr_t)_x) - 1)
#else
#define FROM_PTR(_x) ((void *)(_x))
#define TO_PTR(_x) ((void *)(_x))
#endif

#if (defined(__i386__) || defined(__arm__) || defined(__mips__)) && (SIZEOF_VOIDP == 4)
#define FREELIST_POINTER(_x) (_x).s.pointer
#define FREELIST_VERSION(_x) (_x).s.version
#define SET_FREELIST_POINTER_VERSION(_x, _p, _v) \
(_x).s.pointer = _p; \
(_x).s.version = _v
#elif TS_HAS_128BIT_CAS
#define FREELIST_POINTER(_x) (_x).s.pointer
#define FREELIST_VERSION(_x) (_x).s.version
#define SET_FREELIST_POINTER_VERSION(_x, _p, _v) \
(_x).s.pointer = _p; \
(_x).s.version = _v
#elif defined(__x86_64__) || defined(__ia64__) || defined(__powerpc64__) || defined(__aarch64__) || defined(__mips64)
/* Layout of FREELIST_POINTER
*
* 0 ~ 47 bits : 48 bits, Virtual Address (47 bits for AMD64 and 48 bits for AArch64)
* 48 ~ 62 bits : 15 bits, Freelist Version
* 63 bits : 1 bits, The type of Virtual Address (0 = user space, 1 = kernel space)
*/
/* Detect which shift is implemented by the simple expression ((~0 >> 1) < 0):
*
* If the shift is 'logical' the highest order bit of the left side of the comparison is 0 so the result is positive.
* If the shift is 'arithmetic' the highest order bit of the left side is 1 so the result is negative.
*/
#if ((~0 >> 1) < 0)
/* the shift is `arithmetic' */
#define FREELIST_POINTER(_x) \
((void *)((((intptr_t)(_x).data) & 0x0000FFFFFFFFFFFFLL) | ((((intptr_t)(_x).data) >> 63) << 48))) // sign extend
#else
/* the shift is `logical' */
#define FREELIST_POINTER(_x) \
((void *)((((intptr_t)(_x).data) & 0x0000FFFFFFFFFFFFLL) | (((~((((intptr_t)(_x).data) >> 63) - 1)) >> 48) << 48)))
#endif

#define FREELIST_VERSION(_x) ((((intptr_t)(_x).data) & 0x7FFF000000000000LL) >> 48)
#define SET_FREELIST_POINTER_VERSION(_x, _p, _v) (_x).data = ((((intptr_t)(_p)) & 0x8000FFFFFFFFFFFFLL) | (((_v)&0x7FFFLL) << 48))
#else
#error "unsupported processor"
#endif

struct _InkFreeList {
head_p head;
std::atomic<void *> head{nullptr};
const char *name;
uint32_t type_size, chunk_size, used, allocated, alignment;
std::atomic<uint32_t> used, allocated;
uint32_t type_size, chunk_size, alignment;
uint32_t allocated_base, used_base;
int advice;
};
Expand Down Expand Up @@ -196,22 +75,18 @@ void ink_freelists_snap_baseline();

struct InkAtomicList {
InkAtomicList() {}
head_p head{};
std::atomic<void *> head{nullptr};
const char *name = nullptr;
uint32_t offset = 0;
};

#if !defined(INK_QUEUE_NT)
#define INK_ATOMICLIST_EMPTY(_x) (!(TO_PTR(FREELIST_POINTER((_x.head)))))
#else
/* ink_queue_nt.c doesn't do the FROM/TO pointer swizzling */
#define INK_ATOMICLIST_EMPTY(_x) (!((FREELIST_POINTER((_x.head)))))
#endif
#define INK_ATOMICLIST_EMPTY(_x) (_x.head == nullptr)

inkcoreapi void ink_atomiclist_init(InkAtomicList *l, const char *name, uint32_t offset_to_next);
inkcoreapi void *ink_atomiclist_push(InkAtomicList *l, void *item);
void *ink_atomiclist_pop(InkAtomicList *l);
inkcoreapi void *ink_atomiclist_popall(InkAtomicList *l);
inkcoreapi void *ink_atomiclist_next(InkAtomicList *l, void *item);
/*
* WARNING WARNING WARNING WARNING WARNING WARNING WARNING
* only if only one thread is doing pops it is possible to have a "remove"
Expand Down
126 changes: 126 additions & 0 deletions include/tscore/ver_ptr_kruft.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/** @file

Really really scary code to store a "version" number in unused bits in a pointer.

@section license License

Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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.
*/

#pragma once

/*
For information on the structure of the x86_64 memory map:

http://en.wikipedia.org/wiki/X86-64#Linux

Essentially, in the current 48-bit implementations, the
top bit as well as the lower 47 bits are used, leaving
the upper-but one 16 bits free to be used for the version.
We will use the top-but-one 15 and sign extend when generating
the pointer was required by the standard.
*/

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

void ink_queue_load_64(void *dst, void *src);

#ifdef __x86_64__
#define INK_QUEUE_LD64(dst, src) *((uint64_t *)&(dst)) = *((uint64_t *)&(src))
#else
#define INK_QUEUE_LD64(dst, src) (ink_queue_load_64((void *)&(dst), (void *)&(src)))
#endif

#if TS_HAS_128BIT_CAS
#define INK_QUEUE_LD(dst, src) \
do { \
*(__int128_t *)&(dst) = __sync_val_compare_and_swap((__int128_t *)&(src), 0, 0); \
} while (0)
#else
#define INK_QUEUE_LD(dst, src) INK_QUEUE_LD64(dst, src)
#endif

// Warning: head_p is read and written in multiple threads without a
// lock, use INK_QUEUE_LD to read safely. This type was formerly used as a linked list
// head pointer.
union head_p {
head_p() : data(){};

#if (defined(__i386__) || defined(__arm__) || defined(__mips__)) && (SIZEOF_VOIDP == 4)
typedef int32_t version_type;
typedef int64_t data_type;
#elif TS_HAS_128BIT_CAS
typedef int64_t version_type;
typedef __int128_t data_type;
#else
typedef int64_t version_type;
typedef int64_t data_type;
#endif

struct {
void *pointer;
version_type version;
} s;

data_type data;
};

#if (defined(__i386__) || defined(__arm__) || defined(__mips__)) && (SIZEOF_VOIDP == 4)
#define FREELIST_POINTER(_x) (_x).s.pointer
#define FREELIST_VERSION(_x) (_x).s.version
#define SET_FREELIST_POINTER_VERSION(_x, _p, _v) \
(_x).s.pointer = _p; \
(_x).s.version = _v
#elif TS_HAS_128BIT_CAS
#define FREELIST_POINTER(_x) (_x).s.pointer
#define FREELIST_VERSION(_x) (_x).s.version
#define SET_FREELIST_POINTER_VERSION(_x, _p, _v) \
(_x).s.pointer = _p; \
(_x).s.version = _v
#elif defined(__x86_64__) || defined(__ia64__) || defined(__powerpc64__) || defined(__aarch64__) || defined(__mips64)
/* Layout of FREELIST_POINTER
*
* 0 ~ 47 bits : 48 bits, Virtual Address (47 bits for AMD64 and 48 bits for AArch64)
* 48 ~ 62 bits : 15 bits, Freelist Version
* 63 bits : 1 bits, The type of Virtual Address (0 = user space, 1 = kernel space)
*/
/* Detect which shift is implemented by the simple expression ((~0 >> 1) < 0):
*
* If the shift is 'logical' the highest order bit of the left side of the comparison is 0 so the result is positive.
* If the shift is 'arithmetic' the highest order bit of the left side is 1 so the result is negative.
*/
#if ((~0 >> 1) < 0)
/* the shift is `arithmetic' */
#define FREELIST_POINTER(_x) \
((void *)((((intptr_t)(_x).data) & 0x0000FFFFFFFFFFFFLL) | ((((intptr_t)(_x).data) >> 63) << 48))) // sign extend
#else
/* the shift is `logical' */
#define FREELIST_POINTER(_x) \
((void *)((((intptr_t)(_x).data) & 0x0000FFFFFFFFFFFFLL) | (((~((((intptr_t)(_x).data) >> 63) - 1)) >> 48) << 48)))
#endif

#define FREELIST_VERSION(_x) ((((intptr_t)(_x).data) & 0x7FFF000000000000LL) >> 48)
#define SET_FREELIST_POINTER_VERSION(_x, _p, _v) (_x).data = ((((intptr_t)(_p)) & 0x8000FFFFFFFFFFFFLL) | (((_v)&0x7FFFLL) << 48))
#else
#error "unsupported processor"
#endif

#ifdef __cplusplus
}
#endif /* __cplusplus */
1 change: 1 addition & 0 deletions proxy/logging/LogObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#pragma once

#include "tscore/ink_platform.h"
#include "tscore/ver_ptr_kruft.h"
#include "Log.h"
#include "LogFile.h"
#include "LogFormat.h"
Expand Down
2 changes: 1 addition & 1 deletion src/tscore/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ libtscore_la_SOURCES = \
ink_memory.cc \
ink_mutex.cc \
ink_queue.cc \
ink_queue_utils.cc \
ink_rand.cc \
ink_res_init.cc \
ink_res_mkquery.cc \
Expand Down Expand Up @@ -119,6 +118,7 @@ libtscore_la_SOURCES = \
TextBuffer.cc \
Tokenizer.cc \
ts_file.cc \
ver_ptr_kruft.cc \
Version.cc \
X509HostnameValidator.cc

Expand Down
Loading