Skip to content

Commit

Permalink
new classes for resource management
Browse files Browse the repository at this point in the history
  • Loading branch information
am831 committed Oct 27, 2023
1 parent 34b0092 commit 949ec83
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.error_ptr
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.

#include "fairseq2n/data/image/detail/jpeg_decompress_struct.h"

#include <jpeglib.h>

#include "fairseq2n/exception.h"
#include "fairseq2n/detail/exception.h"

namespace fairseq2n::detail {

jpeg_decompress::jpeg_decompress() {}

jpeg_decompress::~jpeg_decompress() {
jpeg_destroy_decompress(&cinfo);
}

jpeg_decompress_struct& jpeg_decompress::get() {
return cinfo;
}

} // namespace fairseq2n::detail
27 changes: 27 additions & 0 deletions fairseq2n/src/fairseq2n/data/image/detail/jpeg_decompress_struct.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.

#pragma once

#include <jpeglib.h>
#include "fairseq2n/exception.h"

namespace fairseq2n::detail {

class jpeg_decompress {
public:
jpeg_decompress();
~jpeg_decompress();

jpeg_decompress_struct& get();

private:
jpeg_decompress_struct cinfo;
jpeg_decompress(const jpeg_decompress&);
jpeg_decompress& operator=(const jpeg_decompress&);
};

} // namespace fairseq2n::detail
40 changes: 40 additions & 0 deletions fairseq2n/src/fairseq2n/data/image/detail/png_read_struct.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.

#include "fairseq2n/data/image/detail/png_read_struct.h"

#include "fairseq2n/exception.h"
#include "fairseq2n/detail/exception.h"

namespace fairseq2n::detail {

png_read::png_read() : png_ptr(nullptr), info_ptr(nullptr) {
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
if (png_ptr == nullptr) {
throw internal_error("Failed to create PNG read struct.");
}
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == nullptr) {
png_destroy_read_struct(&png_ptr, nullptr, nullptr);
throw internal_error("Failed to create PNG info struct.");
}
}

png_read::~png_read() {
if (png_ptr) {
png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
}
}

png_structp png_read::getPngPtr() const {
return png_ptr;
}

png_infop png_read::getInfoPtr() const {
return info_ptr;
}

} // namespace fairseq2n::detail
28 changes: 28 additions & 0 deletions fairseq2n/src/fairseq2n/data/image/detail/png_read_struct.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.

#pragma once

#include <png.h>

namespace fairseq2n::detail {

class png_read{
public:
png_read();
~png_read();

png_structp getPngPtr() const;
png_infop getInfoPtr() const;

private:
png_structp png_ptr;
png_infop info_ptr;
png_read(const png_read&);
png_read& operator=(const png_read&);
};

} // namespace fairseq2n::detail
42 changes: 18 additions & 24 deletions fairseq2n/src/fairseq2n/data/image/image_decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
#include <cstdint>
#include <exception>
#include <stdexcept>
#include <iostream>

#include <ATen/Functions.h>
#include <ATen/Tensor.h>
#include <setjmp.h>
#include <csetjmp>

#include "fairseq2n/exception.h"
#include "fairseq2n/float.h"
#include "fairseq2n/fmt.h"
#include "fairseq2n/memory.h"
#include "fairseq2n/span.h"
#include "fairseq2n/data/image/detail/png_read_struct.h"
#include "fairseq2n/data/image/detail/jpeg_decompress_struct.h"
#include "fairseq2n/data/detail/tensor_helpers.h"
#include "fairseq2n/detail/exception.h"

Expand Down Expand Up @@ -67,15 +67,9 @@ image_decoder::operator()(data &&d) const
data
image_decoder::decode_png(const memory_block &block) const
{
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
if (png_ptr == nullptr) {
throw_<internal_error>("Failed to create PNG read struct.");
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == nullptr) {
png_destroy_read_struct(&png_ptr, nullptr, nullptr);
throw_<internal_error>("Failed to create PNG info struct.");
}
png_read pngReadStruct;
png_structp png_ptr = pngReadStruct.getPngPtr();
png_infop info_ptr = pngReadStruct.getInfoPtr();

auto data_ptr = png_const_bytep(block.data());
auto data_len = block.size();
Expand Down Expand Up @@ -162,25 +156,25 @@ image_decoder::decode_png(const memory_block &block) const
data
image_decoder::decode_jpeg(const memory_block &block) const
{
jpeg_decompress jpegDecompressStruct;
jpeg_decompress_struct cinfo = jpegDecompressStruct.get();

auto data_ptr = block.data();
auto data_len = block.size();

struct custom_error_mgr {
struct jpeg_error_mgr pub; // Public fields
jmp_buf setjmp_buffer; // Return to caller
};
typedef struct custom_error_mgr * error_ptr;

auto data_ptr = block.data();
auto data_len = block.size();

// Set up decompression process
struct jpeg_decompress_struct cinfo = {};
struct custom_error_mgr jerr = {};
typedef struct custom_error_mgr * error_ptr;
cinfo.err = jpeg_std_error(&jerr.pub);
// error_exit is called by libjpeg when fatal error occurs
// error_exit is called by libjpeg when a fatal error occurs
jerr.pub.error_exit = [](j_common_ptr cinfo) {
// cinfo->err really points to a custom_error_mgr struct, so coerce pointer
error_ptr myerr = (error_ptr) cinfo->err;
(*cinfo->err->output_message) (cinfo);
// Return control to the setjmp point
// Coerce pointer to custom_error_mgr struct
auto myerr = reinterpret_cast<error_ptr>(cinfo->err);
(*cinfo->err->output_message)(cinfo);
// Return control to the setjmp point
longjmp(myerr->setjmp_buffer, 1);

Check failure on line 178 in fairseq2n/src/fairseq2n/data/image/image_decoder.cc

View workflow job for this annotation

GitHub Actions / Lint C++ / Lint

do not call 'longjmp'; consider using exception handling instead [cert-err52-cpp,-warnings-as-errors]
};
// If an error occurs, error_exit will longjmp back to setjmp
Expand Down

0 comments on commit 949ec83

Please sign in to comment.