commit ed07e62f635bd7dffab81169fcea503e917919a5
parent d2664e2e784689b5ac2775c788d2aafb684b46bf
Author: Kyle Milz <kyle@0x30.net>
Date: Mon, 9 Oct 2017 22:02:46 -0600
bin: namespace and rename some classes
- GlBuffer -> gl_buffer
- GlTranslationUnit -> gl_transunit
- ProcessDir -> process_dir
- Mem -> mem, mem_unix
- GlProcessFile -> gl_procfile
Diffstat:
12 files changed, 134 insertions(+), 101 deletions(-)
diff --git a/bin/demo-shader.cc b/bin/demo-shader.cc
@@ -50,7 +50,7 @@ glyph_encode (unsigned int atlas_x , /* 7 bits */
static void
glyph_vertex_encode (double x, double y,
unsigned int corner_x, unsigned int corner_y,
- const glyph_info_t *gi,
+ const citrun::glyph_info_t *gi,
glyph_vertex_t *v)
{
unsigned int encoded = glyph_encode (gi->atlas_x, gi->atlas_y,
@@ -65,7 +65,7 @@ glyph_vertex_encode (double x, double y,
void
demo_shader_add_glyph_vertices (const glyphy_point_t &p,
double font_size,
- glyph_info_t *gi,
+ citrun::glyph_info_t *gi,
std::vector<glyph_vertex_t> *vertices,
glyphy_extents_t *extents)
{
diff --git a/bin/demo-shader.h b/bin/demo-shader.h
@@ -22,7 +22,7 @@
#include <vector>
#include "demo-common.h"
-#include "gl_font.h"
+#include "gl_font.h" // citrun::glyph_info_t
struct glyph_vertex_t {
@@ -37,7 +37,7 @@ struct glyph_vertex_t {
void
demo_shader_add_glyph_vertices (const glyphy_point_t &p,
double font_size,
- glyph_info_t *gi,
+ citrun::glyph_info_t *gi,
std::vector<glyph_vertex_t> *vertices,
glyphy_extents_t *extents);
diff --git a/bin/gl_buffer.cc b/bin/gl_buffer.cc
@@ -19,16 +19,14 @@
#include "gl_buffer.h"
-GlBuffer::GlBuffer() :
- m_refcount(1),
- m_cursor({0, 0})
+citrun::gl_buffer::gl_buffer() : m_refcount(1), m_cursor({0, 0})
{
glGenBuffers(1, &m_buf_name);
clear();
}
#if 0
-GlBuffer::~GlBuffer()
+gl_buffer::~gl_buffer()
{
//if (!buffer || --buffer->refcount)
// return;
@@ -38,13 +36,13 @@ GlBuffer::~GlBuffer()
#endif
void
-GlBuffer::reference()
+citrun::gl_buffer::reference()
{
m_refcount++;
}
void
-GlBuffer::clear()
+citrun::gl_buffer::clear()
{
m_vertices.clear();
glyphy_extents_clear(&m_ink_extents);
@@ -53,7 +51,7 @@ GlBuffer::clear()
}
void
-GlBuffer::extents(glyphy_extents_t *ink, glyphy_extents_t *logical)
+citrun::gl_buffer::extents(glyphy_extents_t *ink, glyphy_extents_t *logical)
{
if (ink)
*ink = m_ink_extents;
@@ -62,21 +60,21 @@ GlBuffer::extents(glyphy_extents_t *ink, glyphy_extents_t *logical)
}
void
-GlBuffer::move_to(const glyphy_point_t *p)
+citrun::gl_buffer::move_to(const glyphy_point_t *p)
{
m_cursor = *p;
}
void
-GlBuffer::current_point(glyphy_point_t *p)
+citrun::gl_buffer::current_point(glyphy_point_t *p)
{
*p = m_cursor;
}
void
-GlBuffer::add_text(const char *utf8, demo_font_t *font, double font_size)
+citrun::gl_buffer::add_text(const char *utf8, citrun::gl_font &font, double font_size)
{
- FT_Face face = demo_font_get_face(font);
+ FT_Face face = font.get_face();
glyphy_point_t top_left = m_cursor;
m_cursor.y += font_size /* * font->ascent */;
unsigned int unicode;
@@ -113,7 +111,7 @@ GlBuffer::add_text(const char *utf8, demo_font_t *font, double font_size)
unsigned int glyph_index = FT_Get_Char_Index(face, unicode);
glyph_info_t gi;
- demo_font_lookup_glyph(font, glyph_index, &gi);
+ font.lookup_glyph(glyph_index, &gi);
/* Let tab operate like it does in editors, 8 spaces. */
if (unicode == '\t') {
@@ -147,7 +145,7 @@ GlBuffer::add_text(const char *utf8, demo_font_t *font, double font_size)
}
void
-GlBuffer::draw()
+citrun::gl_buffer::draw()
{
GLint program;
glGetIntegerv(GL_CURRENT_PROGRAM, &program);
diff --git a/bin/gl_buffer.h b/bin/gl_buffer.h
@@ -22,11 +22,13 @@
#include <vector>
#include "demo-common.h"
-#include "gl_font.h"
+#include "gl_font.h" // citrun::gl_font
#include "demo-shader.h"
-class GlBuffer
+namespace citrun {
+
+class gl_buffer
{
unsigned int m_refcount;
glyphy_point_t m_cursor;
@@ -37,15 +39,16 @@ class GlBuffer
GLuint m_buf_name;
public:
- GlBuffer();
+ gl_buffer();
void reference();
void clear();
void extents(glyphy_extents_t *, glyphy_extents_t *);
void move_to(const glyphy_point_t *);
void current_point(glyphy_point_t *);
- void add_text(const char *, demo_font_t *, double);
+ void add_text(const char *, citrun::gl_font &, double);
void draw();
};
+} // namespace citrun
#endif // GL_BUFFER_H
diff --git a/bin/gl_procfile.cc b/bin/gl_procfile.cc
@@ -18,15 +18,15 @@
#include <cstring> // std::strncmp
#include <sstream> // std::stringstream
-#include "gl_procfile.h"
#include "lib.h" // citrun_major, struct citrun_{node,header}
+#include "gl_procfile.h" // citrun::gl_procfile
//
// Take a filesystem path and memory map its contents. Map at least a header
// structure on top of it.
//
-GlProcessFile::GlProcessFile(std::string const &path, demo_font_t *font) :
+citrun::gl_procfile::gl_procfile(std::string const &path, citrun::gl_font &font, double const &x_off) :
m_mem(path)
{
// Header is always at offset 0.
@@ -36,17 +36,27 @@ GlProcessFile::GlProcessFile(std::string const &path, demo_font_t *font) :
assert(std::strncmp(m_header->magic, "ctrn", 4) == 0);
assert(m_header->major == citrun_major);
- std::stringstream ss;
- ss << "Program: " << m_header->progname << std::endl;
- ss << "Translation Units: " << m_header->units << std::endl;
- ss << "Lines of Code: " << m_header->loc << std::endl;
+ glyphy_point_t orig_pos = { x_off, 0 };
+ m_glbuffer.move_to(&orig_pos);
- m_glbuffer.add_text(ss.str().c_str(), font, 2);
- glyphy_point_t cur_pos;
- m_glbuffer.current_point(&cur_pos);
+ std::stringstream maj;
+ maj << "Name: '" << m_header->progname << "'" << std::endl;
+ maj << "Translation Units: " << m_header->units << std::endl;
+ maj << "Lines of code: " << m_header->loc << std::endl;
+ m_glbuffer.add_text(maj.str().c_str(), font, 3);
- while (!m_mem.at_end())
- m_tus.emplace_back(m_mem, font, cur_pos);
+ std::stringstream min;
+ min << "Working directory: " << m_header->cwd << std::endl;
+ min << "Instrumented with: v" << m_header->major << "." << m_header->minor << std::endl;
+ m_glbuffer.add_text(min.str().c_str(), font, 2);
+
+ glyphy_point_t draw_pos;
+ m_glbuffer.current_point(&draw_pos);
+
+ while (!m_mem.at_end()) {
+ m_tus.emplace_back(m_mem, font, draw_pos);
+ draw_pos.x += 80;
+ }
// Make sure internal increment in TranslationUnit works as intended.
assert(m_mem.at_end_exactly());
@@ -56,15 +66,13 @@ GlProcessFile::GlProcessFile(std::string const &path, demo_font_t *font) :
// Checks if the pid given by the runtime is alive. Prone to race conditions.
//
bool
-GlProcessFile::is_alive() const
+citrun::gl_procfile::is_alive() const
{
- if (kill(m_header->pids[0], 0) == 0)
- return 1;
- return 0;
+ return kill(m_header->pids[0], 0) == 0;
}
-const GlTranslationUnit *
-GlProcessFile::find_tu(std::string const &srcname) const
+const citrun::gl_transunit *
+citrun::gl_procfile::find_tu(std::string const &srcname) const
{
for (auto &i : m_tus)
if (srcname == i.comp_file_path())
@@ -73,7 +81,7 @@ GlProcessFile::find_tu(std::string const &srcname) const
}
void
-GlProcessFile::display()
+citrun::gl_procfile::display()
{
m_glbuffer.draw();
@@ -82,7 +90,7 @@ GlProcessFile::display()
}
glyphy_extents_t
-GlProcessFile::get_extents()
+citrun::gl_procfile::get_extents()
{
glyphy_extents_t extents;
m_glbuffer.extents(NULL, &extents);
diff --git a/bin/gl_procfile.h b/bin/gl_procfile.h
@@ -1,36 +1,41 @@
#include <string> // std::string
#include <vector> // std::vector
-#include "gl_buffer.h" // GlBuffer
-#include "gl_transunit.h" // GlTranslationUnit
+#include "gl_font.h" // citrun::gl_font
+#include "gl_buffer.h" // citrun::gl_buffer
+#include "gl_transunit.h" // citrun::gl_transunit
#ifdef _WIN32
#include "mem_win32.h"
#else
-#include "mem_unix.h" // MemUnix
+#include "mem_unix.h" // citrun::mem_unix
#endif
+namespace citrun {
+
//
// Owns an executing/executed instrumented processes shared memory file and gl
// buffer.
//
-class GlProcessFile
+class gl_procfile
{
struct citrun_header *m_header;
- GlBuffer m_glbuffer;
+ citrun::gl_buffer m_glbuffer;
#ifdef _WIN32
MemWin32 m_mem;
#else
- MemUnix m_mem;
+ citrun::mem_unix m_mem;
#endif
public:
- GlProcessFile(std::string const &, demo_font_t *);
+ gl_procfile(std::string const&, citrun::gl_font &, double const&);
- const GlTranslationUnit *find_tu(std::string const &) const;
+ const citrun::gl_transunit *find_tu(std::string const &) const;
bool is_alive() const;
void display();
glyphy_extents_t get_extents();
- std::vector<GlTranslationUnit> m_tus;
+ std::vector<citrun::gl_transunit> m_tus;
};
+
+} // namespace citrun
diff --git a/bin/gl_transunit.cc b/bin/gl_transunit.cc
@@ -16,17 +16,18 @@
#include <cstring> // std::memcpy
#include <fstream> // std::ifstream
#include <iostream> // std::cerr
+#include <sstream> // std::stringstream
-#include "gl_transunit.h"
#include "lib.h" // struct citrun_node
+#include "gl_transunit.h" // citrun::gl_transunit
//
// Take a pointer to a shared memory region and map data structures on top.
// Automatically increments the pointer once we know how big this region is.
//
-GlTranslationUnit::GlTranslationUnit(Mem &m_mem, demo_font_t *font,
- glyphy_point_t &cur_pos) :
+citrun::gl_transunit::gl_transunit(citrun::mem &m_mem, citrun::gl_font &font,
+ glyphy_point_t &draw_pos) :
m_node(static_cast<struct citrun_node *>(m_mem.get_ptr())),
m_data((unsigned long long *)(m_node + 1)),
m_data_buffer(m_node->size)
@@ -38,11 +39,17 @@ GlTranslationUnit::GlTranslationUnit(Mem &m_mem, demo_font_t *font,
size += m_node->size * sizeof(unsigned long long);
m_mem.increment(size);
- glyphy_point_t next_pos = cur_pos;
- next_pos.x += 80;
- m_glbuffer.move_to(&cur_pos);
+ m_glbuffer.move_to(&draw_pos);
+
+ std::stringstream tu_info;
+ tu_info << "Source file: " << m_node->comp_file_path << std::endl;
+ tu_info << "Lines of code: " << m_node->size << std::endl;
+ m_glbuffer.add_text(tu_info.str().c_str(), font, 2);
- m_glbuffer.add_text(m_node->comp_file_path, font, 1);
+ glyphy_point_t cur_pos;
+ m_glbuffer.current_point(&cur_pos);
+ cur_pos.x = draw_pos.x;
+ m_glbuffer.move_to(&cur_pos);
std::ifstream file_stream(m_node->abs_file_path);
if (file_stream.is_open() == 0) {
@@ -53,11 +60,11 @@ GlTranslationUnit::GlTranslationUnit(Mem &m_mem, demo_font_t *font,
std::string line;
unsigned int i;
for (i = 1; std::getline(file_stream, line); ++i) {
- m_glbuffer.current_point(&cur_pos);
- cur_pos.x = 0;
+ m_glbuffer.add_text(line.c_str(), font, 1);
+ m_glbuffer.current_point(&cur_pos);
+ cur_pos.x = draw_pos.x;
m_glbuffer.move_to(&cur_pos);
- //m_glbuffer.add_text(line.c_str(), font, 1);
}
if (i != m_node->size)
@@ -70,7 +77,7 @@ GlTranslationUnit::GlTranslationUnit(Mem &m_mem, demo_font_t *font,
// ideally)
//
unsigned int
-GlTranslationUnit::num_lines() const
+citrun::gl_transunit::num_lines() const
{
return m_node->size;
}
@@ -79,13 +86,13 @@ GlTranslationUnit::num_lines() const
// Returns the source file path as it was passed to the compiler.
//
std::string
-GlTranslationUnit::comp_file_path() const
+citrun::gl_transunit::comp_file_path() const
{
return std::string(m_node->comp_file_path);
}
glyphy_extents_t
-GlTranslationUnit::get_extents()
+citrun::gl_transunit::get_extents()
{
glyphy_extents_t extents;
m_glbuffer.extents(NULL, &extents);
@@ -97,13 +104,13 @@ GlTranslationUnit::get_extents()
// Copy live executions to secondary buffer. Used for computing deltas later.
//
void
-GlTranslationUnit::save_executions()
+citrun::gl_transunit::save_executions()
{
std::memcpy(&m_data_buffer[0], m_data, m_node->size * sizeof(unsigned long long));
}
void
-GlTranslationUnit::display()
+citrun::gl_transunit::display()
{
m_glbuffer.draw();
}
diff --git a/bin/gl_transunit.h b/bin/gl_transunit.h
@@ -1,22 +1,25 @@
#include <string> // std::string
#include <vector> // std::vector
-#include "gl_buffer.h" // GlBuffer
-#include "mem.h" // Mem
+#include "gl_buffer.h" // citrun::gl_buffer
+#include "gl_font.h" // citrun::gl_font
+#include "mem.h" // citrun::mem
+namespace citrun {
+
//
// Owns a few pages of shared memory and a gl buffer.
//
-class GlTranslationUnit
+class gl_transunit
{
struct citrun_node *m_node;
uint64_t *m_data;
std::vector<uint64_t> m_data_buffer;
- GlBuffer m_glbuffer;
+ citrun::gl_buffer m_glbuffer;
public:
- GlTranslationUnit(Mem &, demo_font_t *, glyphy_point_t &);
+ gl_transunit(citrun::mem &, citrun::gl_font &, glyphy_point_t &);
std::string comp_file_path() const;
unsigned int num_lines() const;
@@ -24,3 +27,5 @@ public:
void display();
glyphy_extents_t get_extents();
};
+
+} // namespace citrun
diff --git a/bin/mem.h b/bin/mem.h
@@ -3,11 +3,14 @@
#include <string> // std::string
+
+namespace citrun {
+
//
// Class that handles operating system independent access to blocks of memory
// that are efficiently sized for the underlying system.
//
-class Mem
+class mem
{
size_t m_off;
@@ -19,12 +22,9 @@ protected:
size_t m_size;
public:
- Mem() :
- m_off(0)
- {};
+ mem() : m_off(0) {};
- void
- increment(size_t size)
+ void increment(size_t size)
{
size_t page_mask;
size_t rounded_size;
@@ -36,23 +36,11 @@ public:
m_off += rounded_size;
}
- void *
- get_ptr()
- {
- return (char *)m_base + m_off;
- }
-
- bool
- at_end()
- {
- return m_off >= m_size;
- }
-
- bool
- at_end_exactly()
- {
- return m_off == m_size;
- }
+ void *get_ptr() { return (char *)m_base + m_off; }
+ bool at_end() { return m_off >= m_size; }
+ bool at_end_exactly() { return m_off == m_size; }
};
+
+} // namespace citrun
#endif // MEM_H
diff --git a/bin/mem_unix.h b/bin/mem_unix.h
@@ -20,10 +20,12 @@
#include <fcntl.h> // O_RDONLY
#include <unistd.h> // getpagesize
-#include "mem.h" // Mem
+#include "mem.h" // citrun::mem
-class MemUnix : public Mem
+namespace citrun {
+
+class mem_unix : public citrun::mem
{
std::string m_path;
int m_fd;
@@ -36,7 +38,7 @@ class MemUnix : public Mem
}
public:
- MemUnix(std::string const &path) :
+ mem_unix(std::string const &path) :
m_path(path),
m_fd(0)
{
@@ -59,3 +61,5 @@ public:
err(1, "mmap");
}
};
+
+} // namespace citrun
diff --git a/bin/process_dir.cc b/bin/process_dir.cc
@@ -5,17 +5,27 @@
#include <cstring>
#include <iostream>
-ProcessDir::ProcessDir()
+#include <sys/stat.h>
+
+
+citrun::process_dir::process_dir()
{
if ((m_procdir = std::getenv("CITRUN_PROCDIR")) == NULL)
m_procdir = "/tmp/citrun/";
- if ((m_dirp = opendir(m_procdir)) == NULL)
- err(1, "opendir");
+ if ((m_dirp = opendir(m_procdir)) == NULL) {
+ if (errno != ENOENT)
+ err(1, "opendir '%s'", m_procdir);
+
+ // Create if there was no such file or directory.
+ mkdir(m_procdir, S_IRWXU);
+ if ((m_dirp = opendir(m_procdir)) == NULL)
+ err(1, "opendir '%s'", m_procdir);
+ }
}
std::vector<std::string>
-ProcessDir::scan()
+citrun::process_dir::scan()
{
std::vector<std::string> new_files;
struct dirent *dp;
diff --git a/bin/process_dir.h b/bin/process_dir.h
@@ -5,13 +5,18 @@
#include <unordered_set>
#include <vector>
-class ProcessDir
+
+namespace citrun {
+
+class process_dir
{
const char *m_procdir;
DIR *m_dirp;
std::unordered_set<std::string> m_known_files;
public:
- ProcessDir();
+ process_dir();
std::vector<std::string> scan();
};
+
+} // namespace citrun