1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CRAZY_LINKER_THREAD_H
6#define CRAZY_LINKER_THREAD_H
7
8#include <stdarg.h>
9#include <stddef.h>
10
11namespace crazy {
12
13// Per-thread context used during crazy linker operations.
14class ThreadData {
15
16 public:
17  ThreadData() {}
18
19  // Init new ThreadData instance.
20  void Init();
21
22  // Return the current error message. This also clears the internal
23  // error message, which means that the next call to this method
24  // will return a pointer to an empty string unless AppendError()
25  // was called.
26  const char* GetError() const { return dlerror_; }
27
28  // Swap the error buffers.
29  void SwapErrorBuffers();
30
31  // Set message string in current dlerror buffer.
32  void SetError(const char* fmt, ...) {
33    va_list args;
34    va_start(args, fmt);
35    SetErrorArgs(fmt, args);
36    va_end(args);
37  }
38
39  void SetErrorArgs(const char* fmt, va_list args);
40
41  // Append message string to current dlerror buffer.
42  void AppendError(const char* fmt, ...) {
43    va_list args;
44    va_start(args, fmt);
45    AppendErrorArgs(fmt, args);
46    va_end(args);
47  }
48
49  void AppendErrorArgs(const char* fmt, va_list args);
50
51 private:
52  // Pointer to the current dlerror buffer. This points to one
53  // of the dlerror_buffers[] arrays, swapped on each dlerror()
54  // call.
55  char* dlerror_;
56
57  // Size of each dlerror message buffer size.
58  static const size_t kBufferSize = 512;
59
60  // Two buffers used to store dlerror messages.
61  char dlerror_buffers_[2][kBufferSize];
62};
63
64// Retrieves the ThreadData structure for the current thread.
65// The first time this is called on a given thread, this creates
66// a fresh new object, so this should never return NULL.
67ThreadData* GetThreadData();
68
69// Faster variant that should only be called when GetThreadData() was
70// called at least once on the current thread.
71ThreadData* GetThreadDataFast();
72
73// Set the linker error string for the current thread.
74void SetLinkerErrorString(const char* str);
75
76// Set the formatted linker error for the current thread.
77void SetLinkerError(const char* fmt, ...);
78
79}  // namespace crazy;
80
81#endif  // CRAZY_LINKER_THREAD_H
82