This repository has been archived by the owner on May 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
2,137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
|
||
INCLUDE_PATH=-Iinclude/ | ||
|
||
COMMON_OBJ = $(patsubst %.cc, %.o, $(wildcard src/*.cc)) | ||
|
||
libcommon.a: $(COMMON_OBJ) $(COMMON_HEADER) | ||
$(AR) -rs $@ $(COMMON_OBJ) | ||
|
||
%.o: %.cc | ||
$(CXX) $(CXXFLAGS) $(INCLUDE_PATH) -c $< -o $@ | ||
|
||
clean: | ||
rm -rf libcommon.a | ||
rm -rf src/*.o |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
// Copyright (c) 2014, Baidu.com, Inc. All Rights Reserved | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
// | ||
// Author: [email protected] | ||
|
||
#ifndef BAIDU_COMMON_ATOMIC_H_ | ||
#define BAIDU_COMMON_ATOMIC_H_ | ||
|
||
namespace baidu { | ||
namespace common { | ||
|
||
/** | ||
* @brief 原子加,返回原值 | ||
* | ||
* @param [in/out] mem 原子变量 | ||
* @param [in] add : 加数 | ||
* @return inline int | ||
* @author yanshiguang02 | ||
* @date 2012/09/09 13:55:38 | ||
**/ | ||
static inline int atomic_add(volatile int *mem, int add) | ||
{ | ||
asm volatile( | ||
"lock xadd %0, (%1);" | ||
: "=a"(add) | ||
: "r"(mem), "a"(add) | ||
: "memory" | ||
); | ||
return add; | ||
} | ||
static inline long atomic_add64(volatile long* mem, long add) | ||
{ | ||
asm volatile ( | ||
"lock xaddq %0, (%1)" | ||
: "=a" (add) | ||
: "r" (mem), "a" (add) | ||
: "memory" | ||
); | ||
return add; | ||
} | ||
|
||
/** | ||
* @brief 原子自增 | ||
* | ||
* @param [in/out] mem : volatile int* | ||
* @return inline void | ||
* @author yanshiguang02 | ||
* @date 2012/09/09 13:56:46 | ||
**/ | ||
static inline void atomic_inc(volatile int *mem) | ||
{ | ||
asm volatile( | ||
"lock incl %0;" | ||
: "=m"(*mem) | ||
: "m"(*mem) | ||
); | ||
} | ||
static inline void atomic_inc64(volatile long *mem) | ||
{ | ||
asm volatile( | ||
"lock incq %0;" | ||
: "=m"(*mem) | ||
: "m"(*mem) | ||
); | ||
} | ||
|
||
/** | ||
* @brief 原子自减 | ||
* | ||
* @param [in/out] mem : volatile int* | ||
* @return inline void | ||
* @author yanshiguang02 | ||
* @date 2012/09/09 13:57:54 | ||
**/ | ||
static inline void atomic_dec(volatile int *mem) | ||
{ | ||
asm volatile( | ||
"lock decl %0;" | ||
: "=m"(*mem) | ||
: "m"(*mem) | ||
); | ||
} | ||
static inline void atomic_dec64(volatile long *mem) | ||
{ | ||
asm volatile( | ||
"lock decq %0;" | ||
: "=m"(*mem) | ||
: "m"(*mem) | ||
); | ||
} | ||
|
||
/** | ||
* @brief swap | ||
* | ||
* @param [in/out] lockword : volatile void* | ||
* @param [in/out] value : int | ||
* @return inline int | ||
* @author yanshiguang02 | ||
* @date 2012/09/09 13:55:25 | ||
**/ | ||
static inline int atomic_swap(volatile void *lockword, int value) | ||
{ | ||
asm volatile( | ||
"lock xchg %0, (%1);" | ||
: "=a"(value) | ||
: "r"(lockword), "a"(value) | ||
: "memory" | ||
); | ||
return value; | ||
} | ||
static inline long atomic_swap64(volatile void *lockword, long value) | ||
{ | ||
asm volatile( | ||
"lock xchg %0, (%1);" | ||
: "=a"(value) | ||
: "r"(lockword), "a"(value) | ||
: "memory" | ||
); | ||
return value; | ||
} | ||
|
||
|
||
/** | ||
* @brief if set | ||
if(*mem == cmp) | ||
*mem = xchg; | ||
else | ||
cmp = *mem; | ||
return cmp; | ||
* | ||
* @param [in/out] mem : volatile void* | ||
* @param [in/out] xchg : int | ||
* @param [in/out] cmp : int | ||
* @return inline int | ||
* @author yanshiguang02 | ||
* @date 2012/09/09 13:54:54 | ||
**/ | ||
static inline int atomic_comp_swap(volatile void *mem, int xchg, int cmp) | ||
{ | ||
asm volatile( | ||
"lock cmpxchg %1, (%2)" | ||
:"=a"(cmp) | ||
:"d"(xchg), "r"(mem), "a"(cmp) | ||
); | ||
return cmp; | ||
} | ||
|
||
/** | ||
* @brief 64位 if set | ||
* | ||
* @param [in/out] mem : volatile void* | ||
* @param [in/out] xchg : long long | ||
* @param [in/out] cmp : long long | ||
* @return inline int | ||
* @author yanshiguang02 | ||
* @date 2012/09/09 13:54:15 | ||
**/ | ||
static inline long atomic_comp_swap64(volatile void *mem, long long xchg, long long cmp) | ||
{ | ||
asm volatile( | ||
"lock cmpxchg %1, (%2)" | ||
:"=a"(cmp) | ||
:"d"(xchg), "r"(mem), "a"(cmp) | ||
); | ||
return cmp; | ||
} | ||
|
||
} // namespace common | ||
} // namespace baidu | ||
|
||
#endif | ||
|
||
/* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Copyright (c) 2015, Baidu.com, Inc. All Rights Reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
// Copyright (c) 2011 The LevelDB Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. See the AUTHORS file for names of contributors. | ||
// | ||
// A Cache is an interface that maps keys to values. It has internal | ||
// synchronization and may be safely accessed concurrently from | ||
// multiple threads. It may automatically evict entries to make room | ||
// for new entries. Values have a specified charge against the cache | ||
// capacity. For example, a cache where the values are variable | ||
// length strings, may use the length of the string as the charge for | ||
// the string. | ||
// | ||
// A builtin cache implementation with a least-recently-used eviction | ||
// policy is provided. Clients may use their own implementations if | ||
// they want something more sophisticated (like scan-resistance, a | ||
// custom eviction policy, variable cache sizing, etc.) | ||
|
||
#ifndef BAIDU_COMMON_CACHE_H_ | ||
#define BAIDU_COMMON_CACHE_H_ | ||
|
||
#include <stdint.h> | ||
#include "slice.h" | ||
|
||
namespace baidu { | ||
namespace common { | ||
|
||
class Cache; | ||
|
||
// Create a new cache with a fixed size capacity. This implementation | ||
// of Cache uses a least-recently-used eviction policy. | ||
extern Cache* NewLRUCache(size_t capacity); | ||
|
||
class Cache { | ||
public: | ||
Cache() { } | ||
|
||
// Destroys all existing entries by calling the "deleter" | ||
// function that was passed to the constructor. | ||
virtual ~Cache(); | ||
|
||
// Opaque handle to an entry stored in the cache. | ||
struct Handle { }; | ||
|
||
// Insert a mapping from key->value into the cache and assign it | ||
// the specified charge against the total cache capacity. | ||
// | ||
// Returns a handle that corresponds to the mapping. The caller | ||
// must call this->Release(handle) when the returned mapping is no | ||
// longer needed. | ||
// | ||
// When the inserted entry is no longer needed, the key and | ||
// value will be passed to "deleter". | ||
virtual Handle* Insert(const Slice& key, void* value, size_t charge, | ||
void (*deleter)(const Slice& key, void* value)) = 0; | ||
|
||
// If the cache has no mapping for "key", returns NULL. | ||
// | ||
// Else return a handle that corresponds to the mapping. The caller | ||
// must call this->Release(handle) when the returned mapping is no | ||
// longer needed. | ||
virtual Handle* Lookup(const Slice& key) = 0; | ||
|
||
// Release a mapping returned by a previous Lookup(). | ||
// REQUIRES: handle must not have been released yet. | ||
// REQUIRES: handle must have been returned by a method on *this. | ||
virtual void Release(Handle* handle) = 0; | ||
|
||
// Return the value encapsulated in a handle returned by a | ||
// successful Lookup(). | ||
// REQUIRES: handle must not have been released yet. | ||
// REQUIRES: handle must have been returned by a method on *this. | ||
virtual void* Value(Handle* handle) = 0; | ||
|
||
// If the cache contains entry for key, erase it. Note that the | ||
// underlying entry will be kept around until all existing handles | ||
// to it have been released. | ||
virtual void Erase(const Slice& key) = 0; | ||
|
||
// Return a new numeric id. May be used by multiple clients who are | ||
// sharing the same cache to partition the key space. Typically the | ||
// client will allocate a new id at startup and prepend the id to | ||
// its cache keys. | ||
virtual uint64_t NewId() = 0; | ||
|
||
private: | ||
void LRU_Remove(Handle* e); | ||
void LRU_Append(Handle* e); | ||
void Unref(Handle* e); | ||
|
||
struct Rep; | ||
Rep* rep_; | ||
|
||
// No copying allowed | ||
Cache(const Cache&); | ||
void operator=(const Cache&); | ||
}; | ||
|
||
} // namespace common | ||
} // namespace baidu | ||
|
||
#endif // BAIDU_COMMON_CACHE_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (c) 2014, Baidu.com, Inc. All Rights Reserved | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
// | ||
// Author: [email protected] | ||
|
||
#ifndef BAIDU_COMMON_COUNTER_H_ | ||
#define BAIDU_COMMON_COUNTER_H_ | ||
|
||
#include "atomic.h" | ||
#include "timer.h" | ||
|
||
namespace baidu { | ||
namespace common { | ||
|
||
class Counter { | ||
volatile int64_t val_; | ||
public: | ||
Counter() : val_(0) {} | ||
int64_t Add(int64_t v) { | ||
return atomic_add64(&val_, v) + v; | ||
} | ||
int64_t Sub(int64_t v) { | ||
return atomic_add64(&val_, -v) - v; | ||
} | ||
int64_t Inc() { | ||
return atomic_add64(&val_, 1) + 1; | ||
} | ||
int64_t Dec() { | ||
return atomic_add64(&val_,-1) - 1; | ||
} | ||
int64_t Get() { | ||
return val_; | ||
} | ||
int64_t Set(int64_t v) { | ||
return atomic_swap64(&val_, v); | ||
} | ||
int64_t Clear() { | ||
return atomic_swap64(&val_, 0); | ||
} | ||
}; | ||
|
||
} // namespace common | ||
} // namespace baidu | ||
|
||
#endif // BAIDU_COMMON_COUNTER_H_ | ||
|
||
/* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright (c) 2015, Baidu.com, Inc. All Rights Reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
// Copyright (c) 2011 The LevelDB Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. See the AUTHORS file for names of contributors. | ||
// | ||
// Simple hash function used for internal data structures | ||
|
||
#ifndef BAIDU_COMMON_HASH_H_ | ||
#define BAIDU_COMMON_HASH_H_ | ||
|
||
#include <stddef.h> | ||
#include <stdint.h> | ||
|
||
namespace baidu { | ||
namespace common { | ||
|
||
uint32_t Hash(const char* data, size_t n, uint32_t seed); | ||
|
||
} // common | ||
} // baidu | ||
|
||
#endif // BAIDU_COMMON_HASH_H_ |
Oops, something went wrong.