库洛
1
// 材质管理器
class TextureManager {
std::unordered_map<std::string, std::shared_ptr<Texture>> _cache;
std::mutex _mutex;
public:
std::shared_ptr<Texture> get(const std::string& name) {
std::lock_guard<std::mutex> lock(_mutex);
auto it = _cache.find(name);
if (it != _cache.end()) {
return it->second;
}
auto texture = std::make_shared<Texture>(name);
_cache[name] = texture;
return texture;
}
void UnloadUnused() {
std::lock_guard<std::mutex> lock(_mutex);
for (auto it = _cache.begin(); it != _cache.end();) {
if (it->second.use_count() == 1) {
it = _cache.erase(it);
} else {
++it;
}
}
}
};
- 为什么用了
std::shared_ptr
而不是std::unique_ptr
? - UnloadUnused 函数的作用是什么?
2
2.1
64 位机器,默认内存对齐。
struct P {
bool isActive;
float x[3];
char name[4];
bool isVisible;
float y[3];
P * xxx;
};
- 请你优化这个结构体的内存布局,减少内存占用。并给出优化前后的内存占用。
2.2
class Point2D {
public:
float x;
float y;
virtual ~Point2D() {}
virtual int Area();
}
int main() {
Point2D p;
memset(&p, 0, sizeof(Point2D));
}